# Cannot use same transaction for delete and set mutations

**URL:** <https://discuss.dgraph.io/t/cannot-use-same-transaction-for-delete-and-set-mutations/8143>\
**Category:** Dgraph Clients\
**Tags:** kind:bug, dgraph-js\
**Created:** [February 1, 2018, 2:51pm UTC](https://discuss.dgraph.io/t/cannot-use-same-transaction-for-delete-and-set-mutations/8143 "2018-02-01T14:51:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [February 1, 2018, 2:51pm UTC](https://discuss.dgraph.io/t/cannot-use-same-transaction-for-delete-and-set-mutations/8143/1 "2018-02-01T14:51:22Z")

</div>

**Moved from GitHub [dgraph-js/21](https://github.com/dgraph-io/dgraph-js/issues/21)**

_Posted by_ [jeffkhull](https://github.com/jeffkhull):

I am trying to write a transaction which deletes from a predicate of type [string], writes back to that same predicate, and then commits both at once.

I have found I’m not able to do that with the library currently. What happens is, the original edge is deleted, but the subsequent one is never written. As an example, I am trying to perform these mutations:

```auto
const txn = client.newTxn()

const mu1 = new dgraph.Mutation()
mu1.setDelNquads(`<0x11194> <property.code> * .`)
txn.mutate(mu1)

const mu2 = new dgraph.Mutation()
mu2.setSetNquads(`<0x11194> <property.code> "LDG" .`)
txn.mutate(mu2)

txn.commit()

```

If I create and commit two separate transactions rather than bundling, the written triple is visible. But if I keep it within one transaction, the result is no data left on the predicate.

Am I using the library correctly?

---

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [February 2, 2018, 4:53am UTC](https://discuss.dgraph.io/t/cannot-use-same-transaction-for-delete-and-set-mutations/8143/2 "2018-02-02T04:53:34Z")

</div>

[pawanrawal](https://github.com/pawanrawal) _commented_ :

Could not reproduce this. Could you share a full example @tamethecomplex? Here is what I tried. I also noticed that you are not checking your errors.

```auto
package main

import (
	"context"
	"fmt"
	"log"
	"testing"

	"google.golang.org/grpc"

	"github.com/dgraph-io/dgraph/client"
	"github.com/dgraph-io/dgraph/protos/api"
	"github.com/dgraph-io/dgraph/x"
)

func TestList(t *testing.T) {
	d, err := grpc.Dial("localhost:9080", grpc.WithInsecure())
	if err != nil {
		log.Fatal(err)
	}

	c := client.NewDgraphClient(
		api.NewDgraphClient(d),
	)

	ctx := context.Background()
	x.Check(c.Alter(ctx, &api.Operation{DropAll: true}))
	x.Check(c.Alter(ctx, &api.Operation{Schema: `
		jurisdiction: [string] .
		id: string @index(exact) .
	`}))

	out := []byte(`{"id":"c3acc43c-c635-475d-8bed-7c3b4f2af37d","type":"concept","term":"myTerm1","jurisdiction":["jur1","jur2"],"definition":"myDef1","lastUpdate":"2018-01-30T11:19:34.150"}`)
	assigned, err := c.NewTxn().Mutate(context.Background(), &api.Mutation{SetJson: out, CommitNow: true})
	x.Check(err)
	uid := assigned.Uids["blank-0"]

	q := `
	{
		concepts(func:eq(id, "c3acc43c-c635-475d-8bed-7c3b4f2af37d")) {
			term
			definition
			jurisdiction
		}
	}
	`

	resp, err := c.NewTxn().Query(ctx, q)
	x.Check(err)
	fmt.Println(string(resp.Json))

	txn := c.NewTxn()
	_, err = txn.Mutate(context.Background(), &api.Mutation{
		DelNquads: []byte(`<` + uid + `> <jurisdiction> * .`),
	})
	x.Check(err)

	resp, err = txn.Query(ctx, q)
	x.Check(err)
	fmt.Println(string(resp.Json))

	_, err = txn.Mutate(context.Background(), &api.Mutation{
		SetNquads: []byte(`<` + uid + `> <jurisdiction> "jur3" .`),
	})
	x.Check(err)
	x.Check(txn.Commit(context.Background()))

	resp, err = c.NewTxn().Query(ctx, q)
	x.Check(err)
	fmt.Println(string(resp.Json))
}

```

---

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [February 2, 2018, 1:36pm UTC](https://discuss.dgraph.io/t/cannot-use-same-transaction-for-delete-and-set-mutations/8143/3 "2018-02-02T13:36:03Z")

</div>

[jeffkhull](https://github.com/jeffkhull) _commented_ :

Hi @pawanrawal, sorry, that was an abbreviated code example just for illustration.

For a complete / easily runnable example, please see:

> <https://github.com/cyphercider/js_issue_repro/tree/master/dgraph-js/issue_21>
>
> //github.com/cyphercider/js\_issue\_repro/tree/master/dgraph-js/issue\_21

Please let me know if the example doesn’t work or if something isn’t clear. Thanks!

---

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [February 5, 2018, 3:55am UTC](https://discuss.dgraph.io/t/cannot-use-same-transaction-for-delete-and-set-mutations/8143/4 "2018-02-05T03:55:20Z")

</div>

[pawanrawal](https://github.com/pawanrawal) _commented_ :

Thanks for the example @tamethecomplex. I can reproduce this issue but strangely it happens approximately 1/10 times. I am working on a fix though this issue belongs to the Dgraph server and not the `dgraph-js` client so I’d say create an issue there.

---

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [February 5, 2018, 3:41pm UTC](https://discuss.dgraph.io/t/cannot-use-same-transaction-for-delete-and-set-mutations/8143/5 "2018-02-05T15:41:58Z")

</div>

[gpahal](https://github.com/gpahal) _commented_ :

Issue created in the main Dgraph repo. [Link](https://github.com/dgraph-io/dgraph/issues/2090)

---

<div class="post-metadata">

**Author:** ![diggy](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/diggy/32/3666_2.png) [@diggy](https://discuss.dgraph.io/u/diggy)\
**Post date:** [July 11, 2020, 4:13am UTC](https://discuss.dgraph.io/t/cannot-use-same-transaction-for-delete-and-set-mutations/8143/6 "2020-07-11T04:13:07Z")

</div>


