Search code examples
pythonneo4jpy2neo

Neo4J / py2neo -- create `Relationship` in transaction?


Outside of a transaction I can do this:

from py2neo import Graph, Node, Relationship
graph = Graph()
graph.create(Relationship(node1, "LINKS_TO", node2))

Can I do something analogous inside a transaction?:

tx = graph.cypher.begin()
tx.append(Relationship(node1, "LINKS_TO", node2))  # This doesn't work

Or do I have to manually write it out as a cypher query?


Solution

  • Ok, got it.

    from py2neo import Graph, Relationship
    from py2neo.cypher import CreateStatement
    
    graph = Graph()
    tx = graph.cypher.begin()
    
    statement = CreateStatement(graph)
    statement.create(Relationship(node1, "LINKS_TO", node2))
    tx.append(statement)
    
    tx.commit()