Cannot use same transaction for delete and set mutations

Moved from GitHub dgraph-js/21

Posted by 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:

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?

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.

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))
}

jeffkhull commented :

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

For a complete / easily runnable example, please see:

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

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.

gpahal commented :

Issue created in the main Dgraph repo. Link