phil_20686
phil_20686

Reputation: 4080

Neo4j Behaviour of Merge

So I have a use case where periodically I update my graph with complex subgraphs. In these subgraphs there will be nodes which are already in the graph, and nodes which are new. I had thought that Merge should do this, but in fact merge appears to create a new node even if there was already a unique node, if the property specifications are not identical.

E.g. on the Neo4j Console, Suppose that I do:

MERGE (a:Crew {name:'Neo', occupation:'The One'})
MERGE (a:Crew {name:'Adam', occupation:'Mechanic'})
CREATE UNIQUE (a)-[r:KNOWS]->(b)
RETURN *

That causes the console to create a second version of Neo, rather than to simple attach the occupation to the existing version.

enter image description here

This happens even if you use:

CREATE CONSTRAINT ON (p:Crew) ASSERT p.name IS UNIQUE

Although now it just refuses to create anything since it won't match the two neo's as one property is blank, and it isn't allowed to create a new node either.

Upvotes: 1

Views: 59

Answers (2)

Michael Hunger
Michael Hunger

Reputation: 41706

Use this instead:

MERGE (a:Crew {name:'Neo'}) ON CREATE SET a.occupation='The One'
MERGE (a:Crew {name:'Adam'}) ON CREATE SET b.occupation='Mechanic'
MERGE (a)-[r:KNOWS]->(b)
RETURN *

See also: http://docs.neo4j.org/refcard/2.1/

Upvotes: 2

BtySgtMajor
BtySgtMajor

Reputation: 1462

It creates a second version because it's not unique. You're specifying an additional property, "occupation", which doesn't currently exist in the original Neo node, so it doesn't find a match and therefore creates a new node.

Upvotes: 2

Related Questions