2

I'm trying to update a node using py2neo as part of a transaction.

The problem is I can't seem to find an equivalent of Graph.push() such as Transaction.Push(). Am I missing something obvious?

My code at the moment looks like this, I'd like to resolve the obvious ???? bit.

def write_to_database( self, t: Transaction ) -> None:

    n = None
    use_existing = False

    # Not part of the transaction:
    n = t.graph.find_one( "Node", "name", self.name( ) )

    if n:
        use_existing = True
    else:
        n = Node(label)
        n[ "name" ] = self.name( )

    n["size"] = self.get_size()


    if use_existing:
        t.??????????????? # Put this in the transaction!
    else:
        t.create( n )

As a use-case point, I'm using the transaction because it appears to run faster for 1000s of operations, not because I require roll-back functionality.

c z
  • 7,726
  • 3
  • 46
  • 59

1 Answers1

0

Your entire method body can be replaced by the following, which runs the equivalent Cypher statement within the transaction:

t.run(
  "MERGE (n:Node {name: {name}}) SET n.size = {size}",
  {"name": self.name(), "size": self.get_size()}
);
cybersam
  • 63,203
  • 6
  • 53
  • 76