How to create a unique node

New to DGraph, hoping this has a simple solution.

I want to create a unique node for an image label, which could be almost anything. So let’s say the label is ‘dog’, I want a node for ‘dog’, and I don’t want two of them. Any time I have an image with a dog in it I’ll draw an edge from the ‘dog’ node to that image, that way I can find related images very easily.

Right now the only approaches I can think of are the following:

  • XID - I can use the label as an XID to get a UID. This would be great if the ID was simple treated as a string, but it’s fingerprinted and so there is no way to avoid collisions, and I would like to start with a schema that won’t bite me later.
  • Single Master - If I make sure that there is only ever a single thing trying to create a node then I can check for it existing first, then create it. This is a bit annoying, although it does work, with the caveat that if I start from a fresh database I’ll get an error because the predicate I look for doesn’t exist, and there isn’t a way to check the error I get back other than a strings.Contains (or similar) and that’s not safe from future changes to dgraph’s error messages.

I feel like there must be a simpler approach, would love some pointers.


@runningwild: From next version xid won’t be fingerprinted. We assign an unique uid internally and store it.

The code is already merged in master. you can use without worrying about collisions.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.