Update does not add uid to api.Assigned uid map, but does update in database

Moved from GitHub dgo/6

Posted by nikkon226:

When I try to update, the update does not add the uid to the api.Assigned map, but does update the database.

// type model {
//     UID string `json:"uid,omitempty"`
//     Name string `json:"name,omitempty"`
// }
input := &model{
     UID: "0x1234",
        Name: "Test",
}
inputJson, err := json.Marshal(input)
if err != nil {
	log.Println("error handing json marshal")
	return nil
}
mu := &api.Mutation{
	SetJson:   inputJson,
	CommitNow: true,
}
log.Println("mutation object:", mu)
 // mutation object: set_json:"{\"uid\":\"0x1234\",\"name\":\"Test\"}" commit_now:true

txn := o.graph.NewTxn()
defer txn.Discard(ctx)
assigned, err := txn.Mutate(ctx, mu)
if err != nil {
	log.Println("error handing mutation for update")
	return nil
}
log.Println("assigned:", assigned.Uids)
// assigned: map[]
// the database has changed the name to "Test"
if _, ok := assigned.GetUids()[input.ID]; !ok {
	log.Println("update unsuccessful")
	return nil
}

manishrjain commented :

Unless the UID has been previously allocated, you shouldn’t be using it. In this case, Dgraph thinks that you’re using an allocated UID, and sets the data. Because the UID field wasn’t empty, it didn’t allocate a UID for you.

Don’t pass the UID field, then Dgraph would allocate a UID for you and return it.

nikkon226 commented :

The uid had been previously allocated by dgraph. This is actually part of a bigger issue (When updating a node, the node's uid isn't returned to signal successful update · Issue #2386 · dgraph-io/dgraph · GitHub) that comes directly from dgraph where dgraph doesn’t include uids of nodes that have been successfully updated.