On Monday, May 29, 2017 at 12:27:51 PM UTC-4, Mike Bayer wrote:
>
> I'm assuming this "invalidates the transaction" is on the Postgresql 
> side, e.g. you get "current transaction is aborted".   There is a simple 
> solution for that which is to use a savepoint, which with the ORM is via 
> session.begin_nested(). 
>
>
> http://docs.sqlalchemy.org/en/latest/orm/session_transaction.html#session-begin-nested
>  
>

Ah, right, SAVEPOINT.  Thanks, that's perfect.  Just a follow up regarding 
the behavior.  The docs provide the following example:

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

It also says each use of begin_nested() must have a corresponding commit() 
or rollback().  I got that the rollback in the example only rolls back to 
the last savepoint (begin_nested()), but what would a commit right after 
session.add(u3) do?  It looks like the non-error case would look like this:
Session = sessionmaker()
session = Session()
session.add(u1)
session.add(u2)

session.begin_nested() # establish a savepoint
session.add(u3)
session.commit()  # ???

session.commit() # ???

what does each commit actually do?

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to