Search code examples
pythonmergeneo4jcypherpy2neo

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


Solution

  • 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.