You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unconditionally flush a session each time an execution takes place #9809
Consider simplifying the session execution logic to always run an autoflush, independently of the fact that an ORM statement is being executed or not.
Since the v2 syntax is using the same paradigm for both core and orm execution, with the addition of the orm enabled select in 1.4 and the orm enabled dml statements (insert, update, delete) in v2, the current logic of conditionally flushing the session only when an orm related thing is executed is a bit hard to properly define, and may cause confusion on what is and what isn't an orm statement.
For example: #9805
need to check on this, but this might need to be a separate "autoflush" bit of logic in session._execute_internal() that's decided after we know there is no ORM context for the statement. When an ORM context exists, the ORM context would still decide when to autoflush. that is, internally this is not actually "simpler". This because I recall that the timing of when the autoflush happens, is fairly specific. I think this has to do with respecting do_orm_execute() event handlers that turn autoflush on or off, which have to run first - #7837 was a big change regarding this.
OTOH if we take a look and see that session._execute_internal() is running these do_orm_execute() event handlers and decide that the actual "self._autoflush()" step can be within _execute_internal() and not in the ORM contexts, that would simplify things.
Activity
zzzeek commentedon May 20, 2023
need to check on this, but this might need to be a separate "autoflush" bit of logic in session._execute_internal() that's decided after we know there is no ORM context for the statement. When an ORM context exists, the ORM context would still decide when to autoflush. that is, internally this is not actually "simpler". This because I recall that the timing of when the autoflush happens, is fairly specific. I think this has to do with respecting do_orm_execute() event handlers that turn autoflush on or off, which have to run first - #7837 was a big change regarding this.
OTOH if we take a look and see that session._execute_internal() is running these do_orm_execute() event handlers and decide that the actual "self._autoflush()" step can be within _execute_internal() and not in the ORM contexts, that would simplify things.
zzzeek commentedon Nov 19, 2024
we're doing the
do_orm_execute()event for non-ORM statements as well so this suggests autoflush should be heresqla-tester commentedon Jul 10, 2025
Mike Bayer has proposed a fix for this issue in the main branch:
Unconditionally flush session on all statement executions https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/6008
Unconditionally flush session on all statement executions