danatron
danatron

Reputation: 717

best way to create a savepoint in sqlalchemy

I have an application where users create cost models. I would like to implement a feature where users can select a "trial" mode which allows them to make as many changes as they wish and observe the cost impact. If the trial is a success the user should be able to commit all changes, if unsuccessful the user should be able to rollback to the state before entering trial mode.

I know I could probably store all of this in the session and simply use commit or rollback, but is that the best practice here? Is there a better mechanism to set and restore the dbs state?

Upvotes: 1

Views: 6065

Answers (1)

user1644807
user1644807

Reputation:

There is example of savepoint usage. You can read more at here

Session = sessionmaker()
session = Session()
session.add(u1)
session.add(u2)

session.begin_nested() # establish a savepoint
session.add(u3)
session.rollback()  # rolls back u3, keeps u1 and u2

session.commit() # commits u1 and u2

Upvotes: 3

Related Questions