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,
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?