Unique constraint

Along with some other nodes I‘d like to have a user node. Every user has an username which has to be unique. Is it possible to achieve this in Dgraph?

2 Likes

If you use graphql APIs, the id Directive is directly supported as a schema option to mark uniqueness for fields like username. https://graphql.dgraph.io/doc/schema/ids

1 Like

I love it. :heart_eyes:

1 Like

But can there be multiple unique fields? E.g. Email also has to be unique.

1 Like

Not supported yet, but in the works. Please see this thread for reference.

1 Like

Thanks.
Just for me to understand it correctly: In every type there might be up to one “@id”. Am I correct?

Yes, that is correct. You can either use the ID type or the @id directive to establish a unique field.

So if a user has an email address I make a type named email and every email has an address with @id.
This is a workaround but until unique gets available. :man_shrugging:t3:

Agreed. Cant think of any better or more elegant way to handle this.

1 Like