Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

py2neo graph.merge() behaves differently from Cypher MERGE?

So, for an empty database MERGE (N1:A {name:"A"})-[:r]->(N2:B {name:"B"}) will create two nodes N1 and N2 with an edge r between them. The following python code however does not do that... but why? Should it not?

from py2neo import Graph, authenticate, rel, Node

graph = Graph()

# set up authentication parameters
authenticate("localhost:7474", <user>, <password>)

# clear the data base
graph.delete_all()

graph.merge(rel(Node("A" , name="A"), "r", Node("B" , name="B")))

Running that script results in a still empty database. Why is that and how can I get the Cypher merge behaviour from py2neo without using graph.cypher.execute("MERGE ...")?

like image 358
lo tolmencre Avatar asked Oct 19 '25 02:10

lo tolmencre


2 Answers

In Py2neo graph.merge matches or creates a single node by label and (optionally) property, where you are wanting to MERGE on the entire pattern (node, relationship, other node).

The pattern you are using for the Cypher MERGE statement does not appear to be supported in Py2neo outside of Cypher.

like image 196
William Lyon Avatar answered Oct 21 '25 15:10

William Lyon


Here is a example on how to merge a relationship of two nodes.

from py2neo import Graph, authenticate, Relationship, Node

server = "localhost:7474"

# set up authentication parameters
authenticate(server, <user>, <password>)

graph = Graph("{0}/db/data".format(server))

# merge nodes and relationship
node1 = Node("A", name="A")
node2 = Node("B", name="B")
node1_vs_node2 = Relationship(node1, "r", node2)
graph.merge(node1_vs_node2)

The result is: Nodes A and B related after a merge

like image 28
Paulo Fabrício Avatar answered Oct 21 '25 16:10

Paulo Fabrício



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!