diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-04-02 20:45:44 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-04-03 13:47:57 -0400 |
commit | c7d3ca0da477451885158a923aa9ee7e49794541 (patch) | |
tree | 9f5255bbec244ef8abce42bd8694aae9631e70a7 /lib/sqlalchemy/orm/loading.py | |
parent | 49b6c50016c8a038a6df7104560bb3945debe064 (diff) | |
download | sqlalchemy-c7d3ca0da477451885158a923aa9ee7e49794541.tar.gz |
Run autoflush for column attribute load operations
The "autoflush" behavior of :class:`.Query` will now trigger for nearly
all ORM level attribute load operations, including when a deferred
column is loaded as well as when an expired column is loaded. Previously,
autoflush on load of expired or unloaded attributes was limited to
relationship-bound attributes only. However, this led to the issue
where column-based attributes that also depended on other rows, or even
other columns in the same row, in order to express the correct value,
would show an effectively stale value when accessed as there could be
pending changes in the session left to be flushed. Autoflush
is now disabled only in some cases where attributes are being unexpired in
the context of a history operation.
Fixes: #5226
Change-Id: Ibd965b30918cd273ae020411a704bf2bb1891f59
Diffstat (limited to 'lib/sqlalchemy/orm/loading.py')
-rw-r--r-- | lib/sqlalchemy/orm/loading.py | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/lib/sqlalchemy/orm/loading.py b/lib/sqlalchemy/orm/loading.py index 49c71e5b2..b7ef96e1a 100644 --- a/lib/sqlalchemy/orm/loading.py +++ b/lib/sqlalchemy/orm/loading.py @@ -194,7 +194,12 @@ def get_from_identity(session, mapper, key, passive): def load_on_ident( - query, key, refresh_state=None, with_for_update=None, only_load_props=None + query, + key, + refresh_state=None, + with_for_update=None, + only_load_props=None, + no_autoflush=False, ): """Load the given identity key from the database.""" if key is not None: @@ -203,6 +208,9 @@ def load_on_ident( else: ident = identity_token = None + if no_autoflush: + query = query.autoflush(False) + return load_on_pk_identity( query, ident, @@ -992,7 +1000,7 @@ class PostLoad(object): pl.loaders[token] = (token, limit_to_mapper, loader_callable, arg, kw) -def load_scalar_attributes(mapper, state, attribute_names): +def load_scalar_attributes(mapper, state, attribute_names, passive): """initiate a column-based attribute refresh operation.""" # assert mapper is _state_mapper(state) @@ -1007,6 +1015,8 @@ def load_scalar_attributes(mapper, state, attribute_names): result = False + no_autoflush = passive & attributes.NO_AUTOFLUSH + # in the case of inheritance, particularly concrete and abstract # concrete inheritance, the class manager might have some keys # of attributes on the superclass that we didn't actually map. @@ -1031,6 +1041,7 @@ def load_scalar_attributes(mapper, state, attribute_names): None, only_load_props=attribute_names, refresh_state=state, + no_autoflush=no_autoflush, ) if result is False: @@ -1068,6 +1079,7 @@ def load_scalar_attributes(mapper, state, attribute_names): identity_key, refresh_state=state, only_load_props=attribute_names, + no_autoflush=no_autoflush, ) # if instance is pending, a refresh operation |