Deleting literal value is not working

Moved from GitHub pydgraph/124

Posted by dtrckd:

Hello,

I am using the following code to do a mutation that should remove one field, and modify another one:

_mu = {                                                                                                                    
        'uid': o['uid']
        'Post.title': None, 
        'Tension.title': o['Post.title']                                                                                                
      }
mu = pydgraph.Mutation(set_json=json.dumps(_mu).encode('utf8))
txn.mutate(mu)

The mutation successfully added the field Tension.title but Post.title is not deleted and instead it is set with the value “null”.

According to the doc, the field should have been deleted.
Am I missing something or it is a normal behavior?

pydgraph==2.0.3

Dgraph version : v1.2.1
Dgraph SHA-256 : 3f18ff84570b2944f4d75f6f508d55d902715c7ca2310799cc2991064eb046f8
Commit SHA-1 : ddcda9296
Commit timestamp : 2020-02-06 15:31:05 -0800
Branch : HEAD
Go version : go1.13.5

martinmr commented :

It seems like an issue. I am guessing is some kind of encoding issue in which the null value gets interpreted as a string. Still not enough information to know where the issue happens.

I’ll get the exact json (the result of running json.dumps(_mu).encode(utf8))`.
Then send that json to dgraph using ratel and see if I get the same result. If so, then it’s either a json issue or a bug in dgraph. If it works now, it’s an issue with the client itself.

Feel free to do those steps on your own. It’d be good to verify it on your own cluster.

martinmr commented :

Sorry for the delay, I dropped this from my plate. I worked on an automated test and found the issue. When you want to delete stuff. You need to set the object in the field del_json not set_json. You cannot delete and add in a single mutation (but you can do it in a single transaction).

So the correct code is

_mu = {                                                                                                                    
        'uid': o['uid']
        'Post.title': None,                                                             
      }
mu = pydgraph.Mutation(del_json=json.dumps(_mu).encode('utf8))
txn.mutate(mu)

I’ll update the documentation to clarify this point.