# Writing only unique nodes based on values not working

**URL:** <https://discuss.dgraph.io/t/writing-only-unique-nodes-based-on-values-not-working/4328>\
**Category:** Users\
**Created:** [April 16, 2019, 2:31pm UTC](https://discuss.dgraph.io/t/writing-only-unique-nodes-based-on-values-not-working/4328 "2019-04-16T14:31:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevenayers](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@stevenayers](https://discuss.dgraph.io/u/stevenayers)\
**Post date:** [April 16, 2019, 2:31pm UTC](https://discuss.dgraph.io/t/writing-only-unique-nodes-based-on-values-not-working/4328/1 "2019-04-16T14:31:38Z")

</div>

I’m noticing when running:

```auto
uid := FunctionToGetUidBasedOnValue(value)
if uid == "" {
    CreateNode(value)
}
err := txn.Commit()
if err != nil {
   txn.Discard()
}

```

I still get duplicate values made of the same `value`. I want nodes to be unique based on `value`.  
I have used debug and found that multiple goroutines running `FunctionToGetUidBasedOnValue(value)` with the same `value` contents are saying that no object exists.

How is this possible?

I thought `txn.Commit()` forced the get query to happen at the same time as the Mutate, and if the get failed, it would fail the Mutate.

---

<div class="post-metadata">

**Author:** ![dmai](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/dmai/32/1254_2.png) [@dmai](https://discuss.dgraph.io/u/dmai)\
**Post date:** [April 16, 2019, 4:37pm UTC](https://discuss.dgraph.io/t/writing-only-unique-nodes-based-on-values-not-working/4328/2 "2019-04-16T16:37:13Z")

</div>

It sounds like you want to upsert an edge here. You can use the `@upsert` directive in your schema to do upserts. [https://docs.dgraph.io/query-language/#upsert-directive](https://docs.dgraph.io/query-language/#upsert-directive)

Commit does not “batch up” all the transaction operations (queries, mutations) in one go (if it did, you wouldn’t get query or mutation responses without calling Commit). Commit tells Dgraph to try and persist all the changes in the commit or abort if there’s a conflict.

---

<div class="post-metadata">

**Author:** ![stevenayers](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@stevenayers](https://discuss.dgraph.io/u/stevenayers)\
**Post date:** [April 16, 2019, 6:00pm UTC](https://discuss.dgraph.io/t/writing-only-unique-nodes-based-on-values-not-working/4328/3 "2019-04-16T18:00:22Z")

</div>

Thanks Daniel. That’s done the trick.

---

<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:** [May 16, 2019, 6:00pm UTC](https://discuss.dgraph.io/t/writing-only-unique-nodes-based-on-values-not-working/4328/4 "2019-05-16T18:00:22Z")

</div>

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