Peeyush Kushwaha
Peeyush Kushwaha

Reputation: 3623

Commas in MERGE clause like there are in MATCH clause?

The following works fine in neo4j 4:

MATCH (a)-->(b)<--(c), (b)-->(d)
RETURN a

But the following returns an error:

MERGE (a)-->(b)<--(c), (b)-->(d)
RETURN a

Error text: Neo.ClientError.Statement.SyntaxError

Invalid input ',': expected whitespace, a relationship pattern, ON, FROM GRAPH, USE GRAPH, CONSTRUCT, LOAD CSV, START, MATCH, UNWIND, MERGE, CREATE UNIQUE, CREATE, SET, DELETE, REMOVE, FOREACH, WITH, CALL, RETURN, UNION, ';' or end of input (line 1, column 22 (offset: 21))
"MERGE (a)-->(b)<--(c), (b)-->(d)"
                     ^

If I understand correctly, merge provides a level of upsert functionality. But is merge more restricted in matching capability than match? How do I merge complex non-linear patterns that require comma separations?

Upvotes: 0

Views: 179

Answers (1)

cybersam
cybersam

Reputation: 66989

The entire MERGE pattern will be created if any item in the pattern does not yet exist. So, to be safe, you must always make sure every MERGE pattern has only one item that might not exist.

This is why it only makes sense for MERGE to support patterns with a single term.

For example, instead of this (which is not legal Cypher, anyway):

MERGE
  (a:Foo {id: 'a'})-[:BAR]->(b:Foo {id: 'b'})<-[:BAR]-(c:Foo {id: 'c'}),
  (b)-[:BAR]->(d:Foo {id: 'd'})
RETURN a

you should actually do this:

MERGE (a:Foo {id: 'a'})
MERGE (b:Foo {id: 'b'})
MERGE (c:Foo {id: 'c'})
MERGE (d:Foo {id: 'd'})
MERGE (a)-[:BAR]->(b)
MERGE (b)<-[:BAR]-(c)
MERGE (b)-[:BAR]->(d)
RETURN a

Upvotes: 1

Related Questions