Unique constraint in DQL

Hi,

is there any way to have unique fields using DQL?

Hi can u explain what do you mean in “unique”?
If you want a predicate (field) to have unique values in each node you can use the @upsert directive and use the upsert mutation instead of regular mutation to insert unique data each time.

1 Like

Thank you for replying.
for example, I want usernames to be unique in my application

So yes the upsert directive + upsert mutations are what you need here.
Follow the link in my last message.
If you need more guidence let me know…

Have a good day
spinelsun

1 Like

why not just supply a directive @unique ?

The main reason is that it is not good to put logic that is too expensive for the whole cluster. And if the user can do it simply and directly using a feature that already exists(And he knows the cost). Adding one more feature is wasting time and resources.

Using Upsert gives the user more control and can do a lot more(custom constraint) by adding logic to the query. So, add a new directive in addition to being expensive is plastered.

Cheers.

1 Like

agree, advisable

Would @id not apply here?

Page not found anymore :smiley: is there a new docs ?

The docs are getting revamped, so some links can get broken.

https://dgraph.io/docs/dql/dql-syntax/dql-mutation/#upsert-block

I believe @gajanan would disagree with this statement, as he talked about hopefully adding a unique directive to the DQL layer.

J