1
votes

I've a neo4j schema in which I've 3 nodes. e.g. p,b,c

I want to write a Merge query such that

MERGE (p)-[:has_b]->(b),
MERGE (p)-[:has_c]->(c1),
MERGE (p)-[:has_c]->(c2)

where c1 and c2 are instance of c node having different property values.

i.e. Merge on all three relationships.

If any of 3 merge queries creates a new node, all relationships should use newly created p node.

I can achieve this if I had only two relationships using

(c)<-[:has_c]-MERGE (p)-[:has_b]->(b)

Any suggestions how to do it for 3 relationships as in my case?

FYI, I'm using py2neo which isn't helping at all.

1

1 Answers

1
votes

Nodes don't have instances. A node is a node and it has a label.

You can MERGE your nodes first to make sure they exist and that all relationships use the same p:

MERGE (p:LabelA {k: "v"})
MERGE (b:LabelB {k: "v"})
MERGE (c1:LabelC {k: "v"})
MERGE (c2:LabelC {k: "v"})
MERGE (p)-[:has_b]->(b)
MERGE (p)-[:has_c]->(c1)
MERGE (p)-[:has_c]->(c2)

This will create the nodes and relationships only once.