Feature request: Directive that indicates that @id fields should be auto-populated on creation

I have two questions:

  • Why is the @id field needed for upserts, what prevents Dgraph from just checking against it’s native uid?
  • Is there any reason why an @id field shouldn’t be/can’t be auto-populated by Dgraph?

On the second point, it’d be great if there was a directive that indicated that an @id field should be auto-populated by Dgraph with a random ID when the node is created. Just seems like the kind of work the database should be doing…?

I think a main use case for it is for uids from other DB’s for syncing purposes, or maybe usernames or something, so those wouldn’t be auto-create scenarios.

But I like the idea.

But it seems like you’d only want to use it if you weren’t satisfied with the native of format and you wanted a different format like integer or uuid for example, no? So which should that be?

You might be able to accomplish it with a @lambda mutation? Not sure there is a onCreate one though.

1 Like

Yeah, there are a lot of variables here. What and how is so broad that it would be hard to make something that works for a variety of use cases without some kind of onCreate

I am assuming this should be under graphql?

Because if you know the id field, it would be an update, not an add - upsert. There is no getPost by @id, and there is no addPost-upsert by ID.

This also has to do with the fact that you can add multiple fields at once, but you can only edit one field at a time. The backend code would have to differentiate between what is a new Post, and what is an update Post. Is there a unique ID or @id? etc…

While this makes sense, I agree that it would be much easier to have one update function that allows you to create, or modify nodes and would automatically match the existing nodes that have an @id or an ID field input.

A lot of the problems Dgraph has not yet solved have already been solved by SQL databases. In this case, we should add an AUTO_INCREMENT @directive.

Something like:

Type {
  uid: ID!
  id: Int! @auto_increment(start: 565)

If you needed to change the start value, just change it in the schema.

Just a thought.