SimplicityGuy
SimplicityGuy

Reputation: 117

Updating a relationship with a merge

I'm using the MERGE functionality to create or update a node, and create a relationship between them as follows:

MATCH (p:Part{f:'fff'})
MERGE (m:Item {p1: {map}.p1})
ON CREATE SET m = {map}
ON MATCH SET m += {map}
MERGE (m)-[:USED_WITH{pri:'High'}]->(p)

However, when I re-run this, but change the property pri on a subsequent run, a new relationship is created. What I want is for the original relationship to be updated.

What's the best practice to do this?

Upvotes: 3

Views: 3936

Answers (1)

albertoperdomo
albertoperdomo

Reputation: 1949

MERGE ensures that the pattern exists exactly as described, including the properties and values, and creates a new one otherwise.

When you change the value of the property pri in the pattern, Cypher doesn't find a match for the pattern because the property value is different, so it creates a new relationship.

You can use MERGE on the relationship type, then use SET to update the property value:

MERGE (m)-[r:USED_WITH]->(p)
SET r.pri = "Low"

Upvotes: 5

Related Questions