Delete a predicate - Dgraph Go Client

Hi there,

I am desperately trying to replace a relation from a node. But it simply adds the relation instead of replacing it. My code looks as follows:

user.TimeFrame = []TimeFrame{
		{
			UID: input.TimeFrameID,
		},
	}
	pb, err := json.Marshal(user)
	if err != nil {
		// error handling
	}

	mu := &api.Mutation{
		CommitNow: true,
		DeleteJson:   pb,
	}

	txn := a.dgraph.NewTxn()
	defer txn.Discard(ctx)

	_, err = txn.Mutate(ctx, mu)

Any help is highly appreciated!

Thanks
Seb

if you’re using JSON obj just see this docs. https://docs.dgraph.io/mutations/#deleting-edges

If you gonna use RDF
https://docs.dgraph.io/mutations/#delete

For golang I guess this can help (I’m not a Go dev)

Thanks for the quick reply. I didn’t find that function because we were using that package here github.com/dgraph-io/dgraph/protos/api. Since it no longer exists I guess it is no longer valid and I should switch to this one: GitHub - dgraph-io/dgo: Official Dgraph Go client ?

Thanks

Yeah, DGo it’s the official Golang Client for Dgraph. Since the 1.0.5 release

see at Improvements

1 Like

hi @MichelDiz

how do i do this:

{
  delete {
     <0x271f> <with_role> * .
  }
}

does this work?

val := &api.NQuad{Subject: "0x271f", Predicate: "with_role"}
val.ObjectValue = &api.Value{Val: &api.Value_DefaultVal{"_STAR_ALL"}}

	sets := []*api.NQuad{
		val,
	}

d.Mutate(ctx, &api.Mutation{
		Del:       sets,
		CommitNow: commit,
	})

and i dont get the different between deleting using json object with id and

{
  delete {
     <0x271f> * * .
  }
}

if i wish todo like above, how do i call it via mutate?
like this?

val := &api.NQuad{Subject: "0x271f", Predicate: "*"}
val.ObjectValue = &api.Value{Val: &api.Value_DefaultVal{"_STAR_ALL"}}

	sets := []*api.NQuad{
		val,
	}

d.Mutate(ctx, &api.Mutation{
		Del:       sets,
		CommitNow: commit,
	})

and one more final issue, i realised after i did the 1st code block like below:

d.Mutate(ctx, &api.Mutation{
		Del:       sets,
		CommitNow: commit,
	})

i can no longer query for user with id 0x271f. but i did was just deleting predicate “with_role” but could not do it. do i need to commit or discard first?

i found out that, ive i mutate delete entire with_role predicate, and then tried to query for with_role only field, it will fail with record not found.

but if i try to query the same record which has no relationships within with_role, as stand alone, it works.
example query

query user() {
  with_role {
    uid
    name
  }
}

but i tried again the delete with_role predicate but this time, i added other field other than with_role on the query, it works!

query user() {
  uid
  with_role {
    uid
    name
  }
}

Maybe this example can help dgraph/main_test.go at 3d0b2cd84f04b263f6c5128e682a63be8dcb220d · dgraph-io/dgraph · GitHub

Commit always before discard.

Since I’m not a GoLang developer. I’m not sure what I can do to help. What I can help is to the limits of my knowledge and using existing examples. But maybe someone else can help. Digging the code is a good one, that’s what I do xD

There is more than one approach to performing a deletion “S P *” or “S * *” - But those recommended in the documentation are the ones indicated for this work.

Cheers.

To Set NQuads I think you need to use like this:

mu.SetNquads = []byte(fmt.Sprintf(<%s> <%s> “%d”^^<xs:int> ., Uid, *pred, Val))

source: https://github.com/dgraph-io/dgraph/blob/master/contrib/integration/increment/main.go

other example: dgraph/main.go at master · dgraph-io/dgraph · GitHub

Other example: dgraph/main.go at master · dgraph-io/dgraph · GitHub

hi @MichelDiz,

i think i did the mutation delete for the predicate and it works properly.
but problem comes after the deletion, the query for with_roles return no record found.
but if i add in uid beside with_roles, and it works.
could this be a bug in dgo library?

Please, can you show the parameters used in the Query (at root)?

About this, everything you’ve reported here is normal. When you make an query requesting fields that no longer exist, the answer will obviously be empty.

the query is as follows:

this works when query, but did not work when i did mutation delete predicate with_role. but it works even after ive deleted if i query as standalone. return record not found if i delete and then query the same query (note, this is before commit , within the same transaction)

query user() {
  with_role {
    uid
    name
  }
}

but with this, it always works, no matter if i delete the predicate then query, or i did a fresh standalone query.

query user() {
  uid
  with_role {
    uid
    name
  }
}

James,

I don’t use go, but deleting is a bit hard to wrap your head around. I have written some tests in Javascript using the JSON delete method here:

Writing the specs helped me understand deletion better.

The helper functions are just thin wrappers around the dgraph api. Maybe copying the specs in Go might help your understanding.

This query is wrong. Functions and parameters are missing.

The differences presented do not help me understand what is happening. Although by what you’ve presenting, if everything matches, you should get expected results. Only more information can enlighten the case.