Revert multiple commits using a savepoint

672 views
Skip to first unread message

kaboo HD

unread,
Jun 19, 2021, 11:27:23 AM6/19/21
to sqlalchemy

I am trying to test an endpoint in flask and I need to "refresh" the DB after some commits.

The idea I had was something like :

db=SQLAlchemy()

db.session.begin_nested()

db.session.add(Obj)
db.session.commit()

# some code where last commit is important

db.session.add(Obj2)
db.session.commit()

# some other code where last commit is important

db.session.rollback()

But it doesn't work unless I have a single commit.

Can you help me please :)

Simon King

unread,
Jun 21, 2021, 5:24:22 AM6/21/21
to sqlal...@googlegroups.com
Which version of SQLAlchemy are you using, and how are you creating
your engine? I believe savepoints are handled differently in SA 1.4 if
you are using the "future-style" engine.

Do these doc links help you at all?

https://docs.sqlalchemy.org/en/14/orm/session_transaction.html#nested-transaction

"""
Calling the Session.commit() or Connection.commit() methods will
always commit the outermost transaction; this is a SQLAlchemy 2.0
specific behavior that is reversed from the 1.x series.
"""

https://docs.sqlalchemy.org/en/14/orm/session_transaction.html#session-subtransactions

"""
The “subtransaction” pattern that was often used with autocommit mode
is also deprecated in 1.4. This pattern allowed the use of the
Session.begin() method when a transaction were already begun,
resulting in a construct called a “subtransaction”, which was
essentially a block that would prevent the Session.commit() method
from actually committing.
"""

Simon
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
> ---
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/d77d4621-8f0a-47fd-8e79-ca3de3a4b5edn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages