Transactions using neomodel

347 Views Asked by At

Based on what is said in the docs the following is available:

@db.transaction
def update_user_name(uid, name):
    user = Person.nodes.filter(uid=uid)[0]
    user.name = name
    user.save()

But how about something more complex like creating a node (like this) and then creating another node, create a relationship between them and read that relationship from one of those nodes somewhere further in the same transaction. I mean what can this library do without commit? I mean normally, using plain py2neo I won't be able to do this as any node created using CreateStatement, which are core part of it's transaction API, isn't bound. It can only be queried only after transaction is commited or processed, but I'm not sure about the latter. Neomodel should be able to do something more since it's wrapping those nodes somehow and caching things - but I cannot be sure - the description of transactions is very short.

There is plenty of code to reimplement so I need to be sure. What's your experiences? What can and what cannot be done in terms of transactions in py2neo / neomodel?

1

There are 1 best solutions below

1
On
from neomodel import db
from demo import A, B


def build_graph():
    db.begin()

    try:
        new_a = A(a_unique_id="AKLKSAJDAD").save()
        new_b = B(b_unique_id="asdawdaw").save()
        new_b.Hates.connect(new_a)
    except:
        return -1

    for rel in new_b.Hates.all():
        print(rel)

    db.commit()