I'm trying to create a constraint with this Cypher query:
CREATE CONSTRAINT ON (p:Person)
ASSERT p.name IS UNIQUE;
But I get the error:
Query failed: Unable to create unique constraint :Person(name), because an existing node violates it.
How can I find which nodes are preventing me from creating a constraint?
MATCH (p:Person)
WITH p.name AS name, collect(p) AS nodes
WHERE size(nodes) > 1
RETURN name, nodes
This query will find all nodes with the label Person and group them by their name property. It will then return any groups that contain more than one node, which indicates that there are multiple nodes with the same name. These are the nodes that are preventing you from creating a unique constraint on the name property.
Once you have identified these nodes, you will need to update them so that their name property is unique. You can do this by either changing their name property to a unique value, or by removing duplicate nodes altogether. Once you have resolved the duplicates, you should be able to create your unique constraint without errors.