mhd21
(mohammad)
February 15, 2021, 8:07am
1
Hi,
is there any way to have unique fields using DQL?
spinelsun
(Or Chen)
February 15, 2021, 11:09am
2
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
mhd21
(mohammad)
February 15, 2021, 6:53pm
3
Thank you for replying.
for example, I want usernames to be unique in my application
spinelsun
(Or Chen)
February 16, 2021, 8:19am
4
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 ?
MichelDiz
(Michel Diz)
September 3, 2021, 3:53pm
6
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
Type1J
(Jay Sistar)
June 2, 2022, 5:12pm
8
Would @id not apply here?
MB175
March 9, 2023, 2:28pm
9
Page not found anymore is there a new docs ?
MichelDiz
(Michel Diz)
March 10, 2023, 3:11am
10
The docs are getting revamped, so some links can get broken.
Raphael
(Raphaël Derbier)
March 23, 2023, 11:20pm
11
jdgamble555
(Jonathan Gamble)
March 24, 2023, 4:12am
12
I believe @gajanan would disagree with this statement, as he talked about hopefully adding a unique directive to the DQL layer.
opened 11:40PM - 02 Nov 22 UTC
closed 06:12PM - 09 Jun 23 UTC
kind/feature
area/querylang
community
### Have you tried Dgraph before this proposal? and did not find anything simila… r?
_No response_
### What you wanted to do.
From the DQL level up, there should be a way to force uniqueness. For backwards compatibility this should be easy. Adding this to a node that already has duplicate values would not be allowed.
This simplest way todo this would be to have a @unique directive in the [dql schema](https://dgraph.io/docs/query-language/schema/):
```graphql
type Film {
name @unique
release_date
revenue
running_time
starring
director
}
```
or with a composite index:
```graphql
type Film @unique(name, release_date) {
name
release_date
revenue
running_time
starring
director
}
```
___
This should also be possible on a 'facet', but there would need to be a way to declare facet types in dql. Perhaps:
```graphql
facet Friend {
close @unique
...
```
and for composite:
```graphql
facet Friend @unique(close, date) {
close
date
}
```
**Note:** In GraphQL, the unique directive would need the "field" input to be not break the GraphQL spec, here we can simplify it with just an array inside ().
### What you actually did.
You can create this in GraphQL, but it is not enforced once added, and DQL is certainly not enforced. Composite indexes are also currently impossible.
### Why wasn't it great, with examples.
We need better data integrity.
### Additional information.
It should be 100% impossible to add any kind of duplicate data once this unique directive is activated.
Unique constraints should be **nullable** by default, but could be not null with the @notnull directive.
This is how @akon-dey wants to implement this. First in DQL, then GraphQL.
**Related to:**
- [GraphQL Composite Uniques](https://github.com/dgraph-io/dgraph/issues/8179)
- [DQL Non Null Constraints](https://github.com/dgraph-io/dgraph/issues/8409)
- [Check Constraints](https://github.com/dgraph-io/dgraph/issues/8410)
J