Skip to content

Commit c7b589c

Browse files
Clarify behavior of MERGE on undirected relationships syntax. (#584)
1 parent f881c3e commit c7b589c

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

modules/ROOT/pages/clauses/merge.adoc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,9 @@ Instead, a new `Movie` node is created.
328328
[[merge-merge-on-an-undirected-relationship]]
329329
=== Merge on an undirected relationship
330330

331-
`MERGE` can also be used with an undirected relationship.
332-
When it needs to create a new one, it will pick a direction.
331+
`MERGE` can also be used without specifying the direction of a relationship.
332+
Cypher will first try to xref:clauses/match.adoc#_match_on_an_undirected_relationship[match the relationship in both directions].
333+
If the relationship does not exist in either direction, it will create one left to right.
333334

334335
.Query
335336
[source, cypher]
@@ -342,7 +343,7 @@ RETURN r
342343
----
343344

344345
As `'Charlie Sheen'` and `'Oliver Stone'` do not know each other in the example graph, this `MERGE` query will create a `KNOWS` relationship between them.
345-
The direction of the created relationship is arbitrary.
346+
The direction of the created relationship is left to right.
346347

347348
.Result
348349
[role="queryresult",options="header",cols="1*<m"]
@@ -534,13 +535,13 @@ However, for relationship uniqueness constraints there are some additional thing
534535

535536
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:
536537

537-
.Query
538+
.Query
538539
[source, cypher, role=test-fail]
539540
----
540541
MERGE (charlie:Person {name: 'Charlie Sheen'})-[r:ACTED_IN {year: 1987}]->(wallStreet:Movie {title: 'Wall Street'})
541542
RETURN charlie.name, type(r), wallStreet.title
542543
----
543-
544+
544545
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.
545546
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.
546547

0 commit comments

Comments
 (0)