Dgraph fails to parse schema using @id directive

I’m the following assumes:
dgraph version: 21.03.1
dgo version: 210

I’ve tagged this as a question because I’m not sure if it’s a bug, or user error. I have a schema for which I’d like to apply the @id directive to a predicate called id. I’d like that predicate to be required, and from what I’ve read, the @id directive should:

  • ensure uniqeness
  • provide a hash index

My use case matches what I’ve read regarding the purpose of the @id directive. I need to store and index external ids which must also have a uniqueness constraint on them.

In my experiments so far, I haven’t been able to successfully lay down schema that leverages the @id directive. When I try to apply the schema below (taken directly from this documentation post: http://discuss.dgraph.io/t/ids-graphql/9766) via grpc using the dgo Alter call…

type User {
    username: string! @id .
}

I get this error: rpc error: code = Unknown desc = line 3 column 22: Expected new line after field declaration. Got @
So in this case it seems like I can’t even apply the directive to the schema

My second experiment is as follows. In my schema, I define an id predicate as follows:
id: string @id .
Then, in the types where it’s required, I attempt to add the predicate as follows:

type Device {
		id
               ...
}

When I attempt to apply this version of the schema, I get the following error:
rpc error: code = Unknown desc = line 2 column 13: Invalid index specification

So, what I can’t figure out here is if the schema parsing is just very picky, and I’m doing something wrong, or if somehow @id is not supported/no longer supported/never was supported. Any feedback on this would be greatly appreciated. Thanks in advance,

 -Nick

Hello Nick and Welcome.
An example of User schema:

type User {
  UserID: ID!
  username: String! @id
}

Hi Poorshad. Thanks. I’m not actually trying to set up a user schema. I’m using a direct example from dgraph’s documentation (here: IDs - Graphql) to test out using the @id directive. I’ll try what you have provided as well, but I could use some more guidance on the @id directive specifically where using the dgo golang library to lay down a schema in a graphdb database.

Ok, as i mentioned below, my purpose here isn’t to create a user schema, but to figure out why the @id directive seems to cause trouble when I try to lay schema down using the Alter call of the dgo golang library to talk to dgraph. When I try to lay down exactly the type you have defined above, this is the result:
rpc error: code = Unknown desc = line 4 column 19: Expected new line after field declaration. Got @

In the schema definition I made with the exact type definition you provided above, line four is username: String! @id. Which returns me to my main question. Am I doing something wrong or is it just not possible to use the @id directive when programmatically laying down schema?

Sorry for misunderstanding, I think the schema in that link is a GraphQL schema and what you want to do is to change schema using Dgraph Go lang client. I’m not using dgo library but here is an example of using it:
https://pkg.go.dev/github.com/dgraph-io/dgo?utm_source=godoc#example-package-GetSchema

Also after we use @id in GraphQL schema and load that graphql schema using CURL I saw that this is how DQL schema changed:

User.username: string @index(hash) @upsert .

The reason why predicate name is User.username because I’m using GraphQL schema and it uses type in first part of the predicate name.