themetalfleece / neogma

Object-Graph-Mapping neo4j framework, Fully-typed with TypeScript, for easy and flexible node and relationship operations
https://themetalfleece.github.io/neogma/
MIT License
122 stars 12 forks source link

Two node using the same primary key #55

Open isaacguerreir opened 1 year ago

isaacguerreir commented 1 year ago

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?

themetalfleece commented 1 year ago

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.

isaacguerreir commented 1 year ago

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

isaacguerreir commented 1 year ago

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

themetalfleece commented 1 year ago

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!