cancel
Showing results for 
Search instead for 
Did you mean: 

MERGE ... ON CREATE... ON MATCH gives an error

mail2michaelenn
Graph Buddy

MERGE (k:Person {name: 'Keanu Reeves'})
ON CREATE
SET k.created = timestamp()
CREATE (k)-[REL]-(m: Movies {name:'M'})
ON MATCH
SET k.lastSeen = timestamp()
CREATE (k)-[REL]-(m: Movies {name:'M'})
RETURN k

The above query gives a syntax error on the 'ON' keyword.... why is this?
in order to get this to work I had to run two MERGE queries...one for k and one for m...
followed by a CREATE relationship. query of (k)---->(m)... which looks inefficient.

2 REPLIES 2

ON CREATE and ON MATCH can right now only do SET operations, you cannot use these to perform conditional cypher. Also, ON CREATE SET ... and ON MATCH SET ... have to be one after the other (order doesn't matter) with no other clauses between them. So your pattern should be:

MERGE (k:Person {name: 'Keanu Reeves'})
ON CREATE SET k.created = timestamp()
ON MATCH SET k.lastSeen = timestamp()
CREATE (k)-[:REL]-(m: Movies {name:'M'})
RETURN k

And that's only because your CREATE of the relationship to a brand new :Movies node with the given name is identical in both cases. If you had wanted some variation in what's created based on whether the MERGE resulted in a MATCH or a CREATE, you wouldn't be able to do that.

This what I realize...thanks for the confirmation.

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.