Unexpected bahaviour when mutating 1-to-1 relations

Hi Sebastian,

Okay, I think I get it*

I did my own test based in yours related to http://discuss.hypermode.com/t/unexpected-bahaviour-when-mutating-1-to-1-relations/5180 · GitHub
the logs are the same

The point is, you have already a child over there right? so you wanna “update” it. To do so, you need to delete the actual child over there and then add a new child. You can’t overwrite it. Dgraph can’t assume that. You should do a proper update procedure.

So, what I would recommend? use Upsert Transaction. Check this tests dgo/upsert_test.go at master · dgraph-io/dgo · GitHub

The Upsert TXN is the fasted way to do what you want. In the Upsert you should use a logic like.

1 - Find the Parent and the Child.
2 - With Parent and Child’s UIDs you use the Child one to delete the relation and delete the Child itself.
e.g:

bulkDelete := `
[
  {
    "uid": "uid(parent)", 
    "child": null # This deletes the relation between parent and child
  },
  {
    "uid": "uid(child)" # This deletes the child itself
  }
]`

3 - You can in the same bulk upsert transaction do one more action. That should be the “update” you wanna do. So you need to understand the multiple block usage in upsert transaction. (I’m not aware of how to do it in Dgo, but looking at the test shared you should be able to find something.)
e.g:

	update := `
[
  {
    "uid": "uid(parent)",
    "child": {
        "uid": "_:NewChild",
        "name": "child replacement"
    }
  }
]`

That’s it. This log you received

rpc error: code = Unknown desc = cannot add value with uid a3948 
to predicate child because one of the existing values does not match 
this uid, either delete the existing values first or modify the schema 
to 'child: [uid]'

only means that Dgraph stopped you from doing a possible mistake.