neogma icon indicating copy to clipboard operation
neogma copied to clipboard

Two node using the same primary key

Open isaacguerreir opened this issue 2 years ago • 4 comments

I created a simple example where field name of a Model is set to be a primary key.

{
  label: 'Vendor',
  schema: {
    name: {
      type: 'string',
      required: true,
      minLength: 1
    },
    description: {
      type: 'string',
      required: true,
      minLength: 5
    },
  },
  primaryKeyField: 'name'
}

In the docs, it says it's a unique identifier.

 /* --> (optional) the key to be used as a unique identifier, which enables some Instance methods */

I made a test and I created two Vendors with the same database. Why it is possible if both of them are unique identifiers? Is it a Neo4J thing?

isaacguerreir avatar Jan 02 '23 02:01 isaacguerreir

Hey Isaac,

I can see how this can be confusing given how I've worded it. What actually happens, is that Neogma will assume that it's unique, and will use it for deleting nodes etc. To make sure that they are actually unique, you need to enforce it, e.g. via a unique index.

Please let me know if it clear things up for you.

themetalfleece avatar Jan 02 '23 09:01 themetalfleece

How could I enforce it in practice (e.g. codewise)?

isaacguerreir avatar Jan 02 '23 17:01 isaacguerreir

Another question is: would be interesting to have an optional in the model to allow neogma return the ID created by Neo4J?

isaacguerreir avatar Jan 02 '23 17:01 isaacguerreir

How could I enforce it in practice (e.g. codewise)?

You can create a unique constraint. So, in your case it would be

CREATE CONSTRAINT vendor_name_unique
FOR (vendor:Vendor) REQUIRE vendor.name IS UNIQUE

But I would recommend providing your own id to the Vendor nodes.

Another question is: would be interesting to have an optional in the model to allow neogma return the ID created by Neo4J?

Yeah this is a good idea!

themetalfleece avatar Jan 03 '23 19:01 themetalfleece