33

I know I can create a unique constraint on a single property with Cypher like CREATE CONSTRAINT ON (p:Person) ASSERT p.name IS UNIQUE. But I was wondering whether it is possible to create a unique constraint which involves multiple properties. If so, how?

3 Answers 3

31

neo4j (2.0.1) does not currently support a uniqueness constraint that covers multiple properties simultaneously.

However, I can think of a workaround that might be acceptable, depending on your use cases. Let's say you want properties a, b, and c to be unique as a group. You can add an extra property, d, that concatenates the stringified values of a, b, and c, using appropriate delimiter(s) to separate the substrings (such that, for example, the a/b delimiter is a character that never appears in a or b). You can then create a uniqueness constraint on d.

[UPDATE]

Neo4j 3.3 added support for uniqueness constraints that cover multiple properties, via node key constraints. However, this feature is only available in the Enterprise Edition.

2
  • 4
    would you know if there are any plans for supporting this? Aug 31, 2014 at 11:12
  • 1
    Hi any update on this? I am not finding this feature in latest version of Neo4j as well. May 30, 2016 at 12:53
28

As of neo4j version 3.3 there is a constraint called NODE KEY which can be used for uniqueness across multiple properties.

From the documentation:

To create a Node Key ensuring that all nodes with a particular label have a set of defined properties whose combined value is unique, and where all properties in the set are present

Example Query

CREATE CONSTRAINT ON (n:Person) ASSERT (n.firstname, n.surname) IS NODE KEY
1
  • 27
    Node Key constraint requires Neo4j Enterprise Edition Sep 1, 2017 at 13:19
5

As of now, neo4j(v3.0.3) still does not support unique constraint with multiple properties. The reason behind this is: When a unique constraint is created it also creates an index on it and as indexes only allow one property, thus constraints can only be applied on one index.

1
  • 1
    neo4j 3.2.1 supports composite index, so it might support composite constraint?
    – digz6666
    Jun 27, 2017 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.