# Upsert not working as expected

**URL:** <https://discuss.dgraph.io/t/upsert-not-working-as-expected/11451>\
**Category:** Users\
**Tags:** kind:question, dgraph\
**Created:** [November 13, 2020, 9:06am UTC](https://discuss.dgraph.io/t/upsert-not-working-as-expected/11451 "2020-11-13T09:06:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ElvizLai](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/elvizlai/32/5276_2.png) [@ElvizLai](https://discuss.dgraph.io/u/ElvizLai)\
**Post date:** [November 13, 2020, 9:06am UTC](https://discuss.dgraph.io/t/upsert-not-working-as-expected/11451/1 "2020-11-13T09:06:37Z")

</div>

schema

```auto
curl localhost:8080/alter -X POST -d $'
name: string @index(term) .
email: string @index(exact) @upsert .
cc: [uid] .
age: int @index(int) .' | jq

```

test code

```auto
func TestZ(t *testing.T) {

	wg := &sync.WaitGroup{}

	for i := 0; i < 20; i++ {
		wg.Add(1)
		q := fmt.Sprintf(`query {
			q(func: eq(email, "user@company.io")) {
			  v1 as uid
			}
		  }`)

		m := fmt.Sprintf(`
		  uid(v1) <name> "first last" .
		  uid(v1) <email> "user@company.io" .
		  `)

		go func() {
			defer wg.Done()
			resp, err := dg.NewTxn().Do(context.Background(), &api.Request{
				Query: q,
				Mutations: []*api.Mutation{{
					SetNquads: []byte(m),
				}},
				CommitNow: true,
			})

			_ = resp
			// if err != nil {
			t.Log(err)
			// }
		}()
	}

	wg.Wait()
}

```

When TestZ first time run, it create 20 nodes with same email.

But when TestZ second time run, only 1 ok, but the others are `Transaction has been aborted. Please retry` (as for doc: this is what we expected)

For data pre-exist or not has different handle strategy.

Any suggest or policy for avoid creating the other 19 node when first time run?

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [November 13, 2020, 4:34pm UTC](https://discuss.dgraph.io/t/upsert-not-working-as-expected/11451/2 "2020-11-13T16:34:49Z")

</div>

Assuming that your code is right. Please do the following.

Erase the p, w, zw folders.

> [@ElvizLai](#):
>
> `@upsert`

Remove this from your Schema. This is a different upsert procedure.

Try again. If that doesn’t work, I’ll review your code again to find any wrong step.

Cheers.

---

<div class="post-metadata">

**Author:** ![ElvizLai](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/elvizlai/32/5276_2.png) [@ElvizLai](https://discuss.dgraph.io/u/ElvizLai)\
**Post date:** [November 13, 2020, 11:12pm UTC](https://discuss.dgraph.io/t/upsert-not-working-as-expected/11451/3 "2020-11-13T23:12:33Z")

</div>

I’m using latest docker image with no data persistence.

```auto
docker pull dgraph/standalone

docker run -it --rm \
    --name dgraph \
    -p 8000:8000 \
    -p 8080:8080 \
    -p 9080:9080 \
    dgraph/standalone

```

And could u pls explain why remove @upsert ?

Opps, can not re-produce.

But remove @upsert, it has same result.

---

<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:** [November 14, 2020, 12:07pm UTC](https://discuss.dgraph.io/t/upsert-not-working-as-expected/11451/4 "2020-11-14T12:07:10Z")

</div>

Hi @ElvizLai

1. Start with a fresh Dgraph runtime, and try without `@upsert` directive on `email` first. You should see 20 email ids (same value) in Dgraph. You should not see any `Transaction has been aborted. Please retry` message. This is what you might have seen in the system.
2. Now, drop data from Dgraph and add the `@upsert` directive on `email`. Run the test again. This time only one node will be created. The `@upsert` directive enforces uniqueness for the `email` field when running transactions concurrently. At this time, you should see `Transaction has been aborted. Please retry`. This is caused by the `@upsert` directive enforcing uniqueness on the `email` attribute.

Please note that the presence of an index is mandatory for `@upsert` to work.
