You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: modules/ROOT/pages/clauses/merge.adoc
+36-5Lines changed: 36 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -415,12 +415,12 @@ This is in contrast to the example shown above in xref::clauses/merge.adoc#merge
415
415
416
416
417
417
[[query-merge-using-unique-constraints]]
418
-
== Using property uniqueness constraints with `MERGE`
418
+
== Using node property uniqueness constraints with `MERGE`
419
419
420
420
Cypher prevents getting conflicting results from `MERGE` when using patterns that involve property uniqueness constraints.
421
421
In this case, there must be at most one node that matches that pattern.
422
422
423
-
For example, given two property uniqueness constraints on `:Person(id)` and `:Person(ssn)`, a query such as `MERGE (n:Person {id: 12, ssn: 437})` will fail, if there are two different nodes (one with `id` 12 and one with `ssn` 437), or if there is only one node with only one of the properties.
423
+
For example, given two property node uniqueness constraints on `:Person(id)` and `:Person(ssn)`, a query such as `MERGE (n:Person {id: 12, ssn: 437})` will fail, if there are two different nodes (one with `id` 12 and one with `ssn` 437), or if there is only one node with only one of the properties.
424
424
In other words, there must be exactly one node that matches the pattern, or no matching nodes.
425
425
426
426
Note that the following examples assume the existence of property uniqueness constraints that have been created using:
@@ -433,9 +433,9 @@ CREATE CONSTRAINT FOR (n:Person) REQUIRE n.role IS UNIQUE;
=== Merge using property uniqueness constraints creates a new node if no node is found
436
+
=== Merge node using property uniqueness constraints creates a new node if no node is found
437
437
438
-
Given the property uniqueness constraint on the `name` property for all `Person` nodes, the below query will create a new `Person` with the `name` property `'Laurence Fishburne'`.
438
+
Given the node property uniqueness constraint on the `name` property for all `Person` nodes, the below query will create a new `Person` with the `name` property `'Laurence Fishburne'`.
439
439
If a `'Laurence Fishburne'` node had already existed, `MERGE` would match the existing node instead.
=== Merge using property uniqueness constraints matches an existing node
457
+
=== Merge using node property uniqueness constraints matches an existing node
458
458
459
459
Given property uniqueness constraint on the `name` property for all `Person` nodes, the below query will match the pre-existing `Person` node with the `name` property `'Oliver Stone'`.
460
460
@@ -526,6 +526,37 @@ While there is a matching unique `Person` node with the name `'Oliver Stone'`, t
526
526
Node already exists with label `Person` and property `name` = 'Oliver Stone'
== Using relationship property uniqueness constraints with `MERGE`
531
+
532
+
All that has been said above about node uniqueness constraints also applies to relationship uniqueness constraints.
533
+
However, for relationship uniqueness constraints there are some additional things to consider.
534
+
535
+
For example, if there exists a relationship uniqueness constraint on `()-[:ACTED_IN(year)]-()`, then the following query, in which not all nodes of the pattern are bound, would fail:
This is due to the all-or-nothing semantics of `MERGE`, which causes the query to fail if there exists a relationship with the given `year` property but there is no match for the full pattern.
545
+
In this example, since no match was found for the pattern, `MERGE` will try to create the full pattern including a relationship with `{year: 1987}`, which will lead to constraint violation error.
546
+
547
+
Therefore, it is advised - especially when relationship uniqueness constraints exist - to always use bound nodes in the `MERGE` pattern.
548
+
The following would, therefore, be a more appropriate composition of the query:
0 commit comments