# How to keep uniqueness

**URL:** <https://discuss.dgraph.io/t/how-to-keep-uniqueness/6740>\
**Category:** Users\
**Created:** [May 15, 2020, 9:29am UTC](https://discuss.dgraph.io/t/how-to-keep-uniqueness/6740 "2020-05-15T09:29:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbn18](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/mbn18/32/1139_2.png) [@mbn18](https://discuss.dgraph.io/u/mbn18)\
**Post date:** [May 15, 2020, 9:29am UTC](https://discuss.dgraph.io/t/how-to-keep-uniqueness/6740/1 "2020-05-15T09:29:17Z")

</div>

I need to insert an entity that must have:

- unique `uuid` predicate (string). Set only once
- unique `account` predicate (string)

Using `upsert` I can verify that the `uuid` does not exist and than insert a new node.  
But how do I verify that the `account` predicate does not exist in other nodes (not necessarily with the same `uuid`)?

Considered:

1. Starting transaction
2. Query for the `UUID`, if exist than abort
3. Query for the `account`, if exist than abort
4. Insert
5. Commit

Or `upsert` with multiple queries??

Thanks

---

<div class="post-metadata">

**Author:** ![mbn18](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/mbn18/32/1139_2.png) [@mbn18](https://discuss.dgraph.io/u/mbn18)\
**Post date:** [May 18, 2020, 11:45am UTC](https://discuss.dgraph.io/t/how-to-keep-uniqueness/6740/2 "2020-05-18T11:45:41Z")

</div>

Sorry for pinging, but uniqueness is quite important.

After 3 days of browsing around I realized that transaction will not help to maintain uniqueness.  
So I am left with `upsert`. But how can I verify that two predicates are unique?

---

<div class="post-metadata">

**Author:** ![JC1236456](https://avatars.discourse-cdn.com/v4/letter/j/8c91f0/32.png) [@JC1236456](https://discuss.dgraph.io/u/JC1236456)\
**Post date:** [May 18, 2020, 3:24pm UTC](https://discuss.dgraph.io/t/how-to-keep-uniqueness/6740/3 "2020-05-18T15:24:13Z")

</div>

Just a noob here, but I would do a conditional upsert with a multiple query block.

[https://dgraph.io/docs/master/mutations/#conditional-upsert](https://dgraph.io/docs/master/mutations/#conditional-upsert)

Someone correct me if I’m wrong please.

---

<div class="post-metadata">

**Author:** ![mbn18](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/mbn18/32/1139_2.png) [@mbn18](https://discuss.dgraph.io/u/mbn18)\
**Post date:** [May 18, 2020, 5:14pm UTC](https://discuss.dgraph.io/t/how-to-keep-uniqueness/6740/4 "2020-05-18T17:14:33Z")

</div>

Yes, this worked for me.

Still not sure if both `uuid` and `account` need the `upsert` index. And if there is any other issue because of the combination of two different predicates.

Here is a working example:

```auto
upsert {
  query {
    idExist(func: eq(uuid, "4dad2587-8537-449f-a3df-786d2907261d")){
      u1 as uid
    }
    acountExist(func: eq(account, "an Account")){
      u2 as uid
    }
  }
  mutation @if(eq(len(u1), 0) and eq(len(u2), 0)){
    set {
      _:tnnt <uuid> "4dad2587-8537-449f-a3df-786d2907261d" .
      _:tnnt <account> "an Account" .
      _:tnnt <dgraph.type> "Whatever" .
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Anurag](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/anurag/32/2599_2.png) [@Anurag](https://discuss.dgraph.io/u/Anurag)\
**Post date:** [May 18, 2020, 6:11pm UTC](https://discuss.dgraph.io/t/how-to-keep-uniqueness/6740/5 "2020-05-18T18:11:48Z")

</div>

Hi @mbn18,  
Can you answer couple of my questions regarding your query:

1. Do you want to update an account number to a given `uuid` (say uuid1) if that account number is already present for some **other** `uuid` (say uuid2)?
2. Would your mutation always add a `uuid` and `account` or would you be possibly only updating account for a given `uuid` if that is already present?

> [@mbn18](#):
>
> Still not sure if both `uuid` and `account` need the `upsert` index. And if there is any other issue because of the combination of two different predicates.

Which index will you use for account and uuid predicates? It is suggested to use `hash` index as discussed [here](https://dgraph.io/docs/tutorial-5/#hash-index). Also both the predicates would require indexing since you will run `eq` operator on both of them.

I don’t foresee any issue because of the combination of two different predicates. Feel free to reach out if you get stuck somewhere.

---

<div class="post-metadata">

**Author:** ![mbn18](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/mbn18/32/1139_2.png) [@mbn18](https://discuss.dgraph.io/u/mbn18)\
**Post date:** [May 19, 2020, 3:49pm UTC](https://discuss.dgraph.io/t/how-to-keep-uniqueness/6740/6 "2020-05-19T15:49:28Z")

</div>

> [@Anurag](#):
>
> - Do you want to update an account number to a given `uuid` (say uuid1) if that account number is already present for some **other** `uuid` (say uuid2)?

No, `account` must be unique across all nodes (its like username. An easy abbreviation of customer name).

> [@Anurag](#):
>
> - Would your mutation always add a `uuid` and `account` or would you be possibly only updating account for a given `uuid` if that is already present?

Current planing is that `uuid` will be immutable while account can be updated. We would however seperate `Add` from `Update`. So this upsert/mutate will take care of the `Add` procedure.

> [@Anurag](#):
>
> Which index will you use for account and uuid predicates?

uuid → `hash` as discussed [here](http://discuss.hypermode.com/t/uuid-efficiency-and-indices/6621)  
account → `exact`

---

<div class="post-metadata">

**Author:** ![Anurag](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/anurag/32/2599_2.png) [@Anurag](https://discuss.dgraph.io/u/Anurag)\
**Post date:** [May 19, 2020, 4:24pm UTC](https://discuss.dgraph.io/t/how-to-keep-uniqueness/6740/7 "2020-05-19T16:24:02Z")

</div>

Cool, I don’t foresee any problem with the proposed query. Like I said you’d need two indexes to make two sub-queries. Indexing can take place in background while you serve queries.

---

<div class="post-metadata">

**Author:** ![mbn18](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/mbn18/32/1139_2.png) [@mbn18](https://discuss.dgraph.io/u/mbn18)\
**Post date:** [May 20, 2020, 6:05am UTC](https://discuss.dgraph.io/t/how-to-keep-uniqueness/6740/8 "2020-05-20T06:05:19Z")

</div>

> [@Anurag](#):
>
> ike I said you’d need two indexes to make two sub-queries.

What I am still not sure about, do I need to add the `upsert` index to both predicates?

---

<div class="post-metadata">

**Author:** ![Anurag](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/anurag/32/2599_2.png) [@Anurag](https://discuss.dgraph.io/u/Anurag)\
**Post date:** [May 20, 2020, 4:18pm UTC](https://discuss.dgraph.io/t/how-to-keep-uniqueness/6740/9 "2020-05-20T16:18:36Z")

</div>

> [@mbn18](#):
>
> What I am still not sure about, do I need to add the `upsert` index to both predicates?

Yes, you would need it on both the predicates to avoid race conditions. Tagging @pawan who is an expert on this.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/2/2b38fdece2abe5814f2e51bee69d1e67fc304b0a.png) [@system](https://discuss.dgraph.io/u/system)\
**Post date:** [June 19, 2020, 11:38pm UTC](https://discuss.dgraph.io/t/how-to-keep-uniqueness/6740/11 "2020-06-19T23:38:13Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
