简体   繁体   中英

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 ...") ?

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.

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: 合并后的节点A和B相关

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM