I’m trying to use an upsert block to increment an ID value on creation so I can have my own internal node IDs that are only unique to other nodes of the same type. I’m basing this on questions like this one which includes the same solution I came up with but it’s not working for me: How to use Dgraph to automatically increase ID?
I have the following manually-created users with my own xid field:
Query:
{
var(func: type(User), first:-1) {
lastid as xid
newid as math(lastid + 1)
}
q(func: type(User)) {
xid
FirstName
val(newid)
}
}
The above upsert mutation works but the xid is never set which I assume means it’s empty when executing the mutation block. How do I get my newid value into the mutation block where I need it?
The first query which returns my users seems to suggest the value only exists in the context of a specific node since the newid value isn’t shown on the “Chris” node which is why it seems to be empty during my mutation.
I think relying on sorting of UIDs to get the latest inserted user would be a bad approach. We assign UIDs in batches and a later inserted user could potentially have lower UID. A better approach may be to keep a separate predicate and store the latest ingested XID there, like a counter and use that instead. This can have some negative impact on performance, though, happy to explain more if you have any question with the approach.
I’m not relying on the UIDs. That’s just an example from the linked post. I’m actually using orderdesc: xid, first:1 to get the last saved xid. My problem is the variable not being recognized in the mutation block which wouldn’t be solved by changing how it’s stored.