# Unique constraint

**URL:** <https://discuss.dgraph.io/t/unique-constraint/9238>\
**Category:** GraphQL\
**Created:** [August 5, 2020, 10:22am UTC](https://discuss.dgraph.io/t/unique-constraint/9238 "2020-08-05T10:22:10Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wulfheart](https://avatars.discourse-cdn.com/v4/letter/w/bcef8e/32.png) [@Wulfheart](https://discuss.dgraph.io/u/Wulfheart)\
**Post date:** [August 5, 2020, 10:22am UTC](https://discuss.dgraph.io/t/unique-constraint/9238/1 "2020-08-05T10:22:10Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![anand](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/anand/32/5674_2.png) [@anand](https://discuss.dgraph.io/u/anand)\
**Post date:** [August 5, 2020, 11:08am UTC](https://discuss.dgraph.io/t/unique-constraint/9238/2 "2020-08-05T11:08:30Z")

</div>

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](https://graphql.dgraph.io/doc/schema/ids)

---

<div class="post-metadata">

**Author:** ![Wulfheart](https://avatars.discourse-cdn.com/v4/letter/w/bcef8e/32.png) [@Wulfheart](https://discuss.dgraph.io/u/Wulfheart)\
**Post date:** [August 5, 2020, 11:19am UTC](https://discuss.dgraph.io/t/unique-constraint/9238/3 "2020-08-05T11:19:47Z")

</div>

I love it. 😍

---

<div class="post-metadata">

**Author:** ![Wulfheart](https://avatars.discourse-cdn.com/v4/letter/w/bcef8e/32.png) [@Wulfheart](https://discuss.dgraph.io/u/Wulfheart)\
**Post date:** [August 5, 2020, 11:23am UTC](https://discuss.dgraph.io/t/unique-constraint/9238/4 "2020-08-05T11:23:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anand](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/anand/32/5674_2.png) [@anand](https://discuss.dgraph.io/u/anand)\
**Post date:** [August 5, 2020, 11:38am UTC](https://discuss.dgraph.io/t/unique-constraint/9238/5 "2020-08-05T11:38:55Z")

</div>

Not supported yet, but in the works. Please see this [thread](http://discuss.hypermode.com/t/support-multiple-unique-fields-in-dgraph-graphql/8512) for reference.

---

<div class="post-metadata">

**Author:** ![Wulfheart](https://avatars.discourse-cdn.com/v4/letter/w/bcef8e/32.png) [@Wulfheart](https://discuss.dgraph.io/u/Wulfheart)\
**Post date:** [August 5, 2020, 11:44am UTC](https://discuss.dgraph.io/t/unique-constraint/9238/6 "2020-08-05T11:44:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anand](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/anand/32/5674_2.png) [@anand](https://discuss.dgraph.io/u/anand)\
**Post date:** [August 5, 2020, 11:59am UTC](https://discuss.dgraph.io/t/unique-constraint/9238/7 "2020-08-05T11:59:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wulfheart](https://avatars.discourse-cdn.com/v4/letter/w/bcef8e/32.png) [@Wulfheart](https://discuss.dgraph.io/u/Wulfheart)\
**Post date:** [August 5, 2020, 12:08pm UTC](https://discuss.dgraph.io/t/unique-constraint/9238/8 "2020-08-05T12:08:48Z")

</div>

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. 🤷🏼‍♂️

---

<div class="post-metadata">

**Author:** ![anand](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/anand/32/5674_2.png) [@anand](https://discuss.dgraph.io/u/anand)\
**Post date:** [August 5, 2020, 12:22pm UTC](https://discuss.dgraph.io/t/unique-constraint/9238/9 "2020-08-05T12:22:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Fizzi](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/fizzi/32/8427_2.png) [@Fizzi](https://discuss.dgraph.io/u/Fizzi)\
**Post date:** [October 28, 2021, 6:37pm UTC](https://discuss.dgraph.io/t/unique-constraint/9238/10 "2021-10-28T18:37:07Z")

</div>

Sorry for bumping a fairly old topic, but this is the first result when searching for dgraph unique constraints on Google.

While the suggested method does sort of work, it has a bit of annoying drawback from what I can tell.

Say I set up a one-to-one relationship for my users to have a unique code:

```auto
type User {
  code: UniqueCode @hasInverse(field: user)
}

type UniqueCode {
  code: String! @id
  user: User!
}

```

Now say I create a new User and UniqueCode using the `addUser` mutation. And the UniqueCode is “CODE1”.

If I then try to create a second user and give it the same code as the previous user, I feel like the behavior I would like for a “true” unique constraint is an error. The behavior dgraph seems to have here though is to steal the UniqueCode reference from the first user and assign it to the second.

I’m not fully sure how to work around this at the moment.

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [October 28, 2021, 10:34pm UTC](https://discuss.dgraph.io/t/unique-constraint/9238/11 "2021-10-28T22:34:00Z")

</div>

If you notice the `@id` directive signifying the uniqueness is on the UniqueCode type. So across all of the nodes having the type UniqueCode, the db will attempt uniqueness through the GraphQL API.

Howver, there is nothing on the user that says it has to throw an error if using an existing UniqueCode vs a new one. This being a one-to-one relationship when you use an existing UniqueCode the API assumes you want it unmapped from the previous and mapped to the new one. There are use cases for this.

Why are you not putting the UniqueCode as a field of the User instead of a 1:1 edge? Maybe you have more fields in your actual use case.

---

<div class="post-metadata">

**Author:** ![Fizzi](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/fizzi/32/8427_2.png) [@Fizzi](https://discuss.dgraph.io/u/Fizzi)\
**Post date:** [October 29, 2021, 1:35pm UTC](https://discuss.dgraph.io/t/unique-constraint/9238/12 "2021-10-29T13:35:45Z")

</div>

I’m doing it as a reference in order to enforce uniqueness. Is there a way to have it directly on User and enforce that codes must be unique across all users? (Without setting it as an ID on user, codes are nullable and User already has a different ID)

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [October 29, 2021, 10:06pm UTC](https://discuss.dgraph.io/t/unique-constraint/9238/13 "2021-10-29T22:06:50Z")

</div>

Depending on your version, you can have multiple @id directives in one type

---

<div class="post-metadata">

**Author:** ![Fizzi](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/fizzi/32/8427_2.png) [@Fizzi](https://discuss.dgraph.io/u/Fizzi)\
**Post date:** [November 2, 2021, 11:14pm UTC](https://discuss.dgraph.io/t/unique-constraint/9238/14 "2021-11-02T23:14:32Z")

</div>

Thanks for the suggestion @amaster507 .

In my use case though, the unique code is actually nullable. I don’t think it’s possible to use `String` instead of `String!` for an @id field correct?

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [November 3, 2021, 12:04am UTC](https://discuss.dgraph.io/t/unique-constraint/9238/15 "2021-11-03T00:04:35Z")

</div>

Correct.
