naraesk
naraesk

Reputation: 75

Neo4J unique constraint for multiple labels

I have a set of nodes with multiple Labels (A, B, C). All nodes have a common property, which is unique across all labels. However, when creating unique constraint it is limited to one label, isn't it?

Documentation says something like:

CREATE CONSTRAINT ON (n:A) ASSERT n.uid IS UNIQUE

But I'd like to do something like

CREATE CONSTRAINT ON (n:A AND n:B AND n:C) ASSERT n.uid IS UNIQUE

or

CREATE CONSTRAINT ON (n) ASSERT n.uid IS UNIQUE

If that is not possible, would it be best, to create a label D, and add it to all nodes with label A, B, and C and then create the constraint for label D?

Upvotes: 7

Views: 2071

Answers (1)

InverseFalcon
InverseFalcon

Reputation: 30397

Your suggestion is exactly what I used in a similar case. I created a label meant to encompass two other labels, and added the constraint on the new one (in addition to the others).

The only trick is remembering to apply that label in addition to any new nodes you create with the sublabels.

It wouldn't be a bad idea to make a neo4j feature request for constraints that apply across multiple labels, that would be rather useful.

Upvotes: 5

Related Questions