I’m trying to do an upsert
using Go client based on the official README and godoc, but it seems to be creating duplicate entries in the database o_O
This is a short snippet of my code:
query := `
query Node($xid: string){
node(func: eq(xid, $xid)) {
xid
}
}
`
node := &Node{
UID: obj.UID().String(),
Name: obj.Name(),
Kind: obj.Kind(),
Ns: obj.Namespace(),
DType: []string{"Object"},
}
pb, err := json.Marshal(node)
if err != nil {
return nil, err
}
mu := &api.Mutation{
SetJson: pb,
}
req := &api.Request{
Query: query,
Vars: map[string]string{"$xid": obj.UID().String()},
Mutations: []*api.Mutation{mu},
CommitNow: true,
}
ctx := context.Background()
txn := d.client.NewTxn()
defer txn.Discard(ctx)
if _, err := txn.Do(ctx, req); err != nil {
return nil, err
}
The idea is to create the Object
only if it does not exist, but the above keeps creating duplicate entries. Am I missing something here? This is the JSON result
{
"data": {
"node": [
{
"xid": "objectUID",
"name": "fooKind-objectX",
"kind": "fooKind",
"ns": "fooNS"
},
{
"xid": "objectUID",
"name": "fooKind-objectX",
"kind": "fooKind",
"ns": "fooNS"
}
]
},
Thanks