1
Relationship direction is important when MERGE
kevin@matterline.io
Hey community,
I've just discovered that the direction of a merge relationship is important:
1.
MATCH (team :Team) WHERE ...
WITH team
MERGE (user :User { email: 'neo@gmail.com' })-[act :ACTING_BY]->(team)
Creates or updates the 'user' and 'act', whereas:
2.
MERGE (user :User { email: 'neo@gmail.com' })<-[act :ACTING_BY]-(team)
Always creates a new 'user' and 'act'
Is there any specific reason?
(PS: I use the latest Aura version)
scott@s4s.com
Scary. Unintended duplication of nodes is my biggest worry as a neo4j neophyte.
Michael Hunger
it is better to separate out node MERGE from relationship MERGE
...
MERGE (user :User { email: 'neo@gmail.com' })
MERGE (user)<-[act :ACTING_BY]-(team)
otherwise the MERGE happens in the "context" of the bound node (team in your case)
Gregory King
Hi Kevin,
Have a review of the
MERGE
knowledgebase article here to see if it clears things up.Quoting from the article:
> The MERGE clause ensures that a pattern exists in the graph. Either the entire pattern already exists, or the entire pattern needs to be created.
> In this way, it’s helpful to think of MERGE as attempting a MATCH on the pattern, and if no match is found, a CREATE of the pattern.
> When the specified pattern is not present and needs to be created, any variables previously bound to existing graph elements will be reused in the pattern. All other elements of the pattern will be created.
> It’s important to know which pattern elements will use existing graph elements and which will be created instead.
If you still have some questions after this, feel free to send a small full example with
CREATE
for a small dataset and we'll see if we can help you out.Cheers,
Greg