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

Viewed 2305

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

2 Answers

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

Related