Skip to content

Unconditionally flush a session each time an execution takes place #9809

Closed

Description

@CaselIT

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

Activity

  1. added this to the 2.1 milestone on May 20, 2023
  2. zzzeek commented on May 20, 2023

    @zzzeek
    Member

    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.

  3. zzzeek commented on Nov 19, 2024

    @zzzeek
    Member

    we're doing the do_orm_execute() event for non-ORM statements as well so this suggests autoflush should be here

  4. sqla-tester commented on Jul 10, 2025

    @sqla-tester
    Collaborator

    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

  5. added a commit that references this issue on Jul 14, 2025
    45a5c31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions