On Mon, Dec 2, 2019, at 2:02 PM, Lee Doolan wrote:
> I recently encountered this little snippet of code at the beginning of a 
> transaction
> 
> # I am surprised we need to call both of these, but
> # we do. Neither alone will prevent stale data from
> # persisting in the session.
>  meta.Session.expunge_all()
>  meta.Session.rollback()
> 
> 
> I have looked through the SQLA documentation (1.3) and I cannot find any 
> reason 
> why simply using a rollback() would not suffice here. Maybe it was necessary 
> when the code
> was written for an older version of SQLA.
> 
> Does anyone have any thoughts on the matter?
> 

Hi there -

expunge_all() and rollback() do different things, and both have a purpose. As 
far as what will "suffice" it depends on what exactly it is that you are 
attempting to accomplish.

I would also note that calling session.close() has the same effect as that of 
the two statements above. 

A typical reason one might want to use both is so that any Python objects which 
are attached to this Session are no longer attached, and as such when their 
unloaded attributes are accessed, no SQL statement or implicit transaction will 
be started again on the original Session.

Documentation for this process at 
https://docs.sqlalchemy.org/en/13/orm/session_basics.html#when-do-i-construct-a-session-when-do-i-commit-it-and-when-do-i-close-it






> 
> 

> --
>  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 view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/55af2158-c110-40e8-b79e-ff320dafeb6a%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/sqlalchemy/55af2158-c110-40e8-b79e-ff320dafeb6a%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/b9ae86a6-6336-4281-b8fc-4a9e7d70cc0f%40www.fastmail.com.

Reply via email to