neogma
neogma copied to clipboard
Two node using the same primary key
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?
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.
How could I enforce it in practice (e.g. codewise)?
Another question is: would be interesting to have an optional in the model to allow neogma return the ID created by Neo4J?
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!