This is what you should do.
The @upsert
directive checks for conflicts for concurrently running txns mutating the same data. It does not check for existing values that have already been committed. This is useful to ensure that the first insert is unique.
The following example should clarify what @upsert
does. TxnA and TxnB are concurrent transactions both running mutation to the same predicate and value—e.g., _:node <foo> "name" .
. The txn operations can be represented serially over Time. Whichever txn commits first succeeds.
With @upsert
:
Time TxnA TxnB
1 NewTxn
2 NewTxn
3 mutate
4 mutate
5 commit (success)
6 commit (abort)
Without @upsert
:
Time TxnA TxnB
1 NewTxn
2 NewTxn
3 mutate
4 mutate
5 commit (success)
6 commit (success)
In v1.1 Dgraph will have a upsert block that allows you to do a query-and-mutate within a single call: New upsert block by mangalaman93 · Pull Request #3412 · dgraph-io/dgraph · GitHub