From 10b7937bb9994c365436af5e0c1931b2b07d12b1 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 31 Jan 2020 11:10:08 -0500 Subject: Warn for runid changing in load events; add restore_load_context flag Added a new flag :paramref:`.InstanceEvents.restore_load_context` and :paramref:`.SessionEvents.restore_load_context` which apply to the :meth:`.InstanceEvents.load`, :meth:`.InstanceEvents.refresh`, and :meth:`.SessionEvents.loaded_as_persistent` events, which when set will restore the "load context" of the object after the event hook has been called. This ensures that the object remains within the "loader context" of the load operation that is already ongoing, rather than the object being transferred to a new load context due to refresh operations which may have occurred in the event. A warning is now emitted when this condition occurs, which recommends use of the flag to resolve this case. The flag is "opt-in" so that there is no risk introduced to existing applications. The change additionally adds support for the ``raw=True`` flag to session lifecycle events. Fixes: #5129 Change-Id: I2912f48ac8c5636297d63ed383454930e8e9a6a3 --- lib/sqlalchemy/orm/session.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'lib/sqlalchemy/orm/session.py') diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py index fe0640a47..9342c3145 100644 --- a/lib/sqlalchemy/orm/session.py +++ b/lib/sqlalchemy/orm/session.py @@ -1975,7 +1975,7 @@ class Session(_SessionClassMethods): if pending_to_persistent is not None: for state in states.intersection(self._new): - pending_to_persistent(self, state.obj()) + pending_to_persistent(self, state) # remove from new last, might be the last strong ref for state in set(states).intersection(self._new): @@ -1998,7 +1998,7 @@ class Session(_SessionClassMethods): if persistent_to_deleted is not None: # get a strong reference before we pop out of # self._deleted - obj = state.obj() + obj = state.obj() # noqa self.identity_map.safe_discard(state) self._deleted.pop(state, None) @@ -2007,7 +2007,7 @@ class Session(_SessionClassMethods): # is still in the transaction snapshot and needs to be # tracked as part of that if persistent_to_deleted is not None: - persistent_to_deleted(self, obj) + persistent_to_deleted(self, state) def add(self, instance, _warn=True): """Place an object in the ``Session``. @@ -2384,7 +2384,7 @@ class Session(_SessionClassMethods): if to_attach: self._after_attach(state, obj) elif revert_deletion: - self.dispatch.deleted_to_persistent(self, obj) + self.dispatch.deleted_to_persistent(self, state) def _save_or_update_impl(self, state): if state.key is None: @@ -2466,7 +2466,7 @@ class Session(_SessionClassMethods): % (state_str(state), state.session_id, self.hash_key) ) - self.dispatch.before_attach(self, obj) + self.dispatch.before_attach(self, state) return True @@ -2474,12 +2474,12 @@ class Session(_SessionClassMethods): state.session_id = self.hash_key if state.modified and state._strong_obj is None: state._strong_obj = obj - self.dispatch.after_attach(self, obj) + self.dispatch.after_attach(self, state) if state.key: - self.dispatch.detached_to_persistent(self, obj) + self.dispatch.detached_to_persistent(self, state) else: - self.dispatch.transient_to_pending(self, obj) + self.dispatch.transient_to_pending(self, state) def __contains__(self, instance): """Return True if the instance is associated with this session. -- cgit v1.2.1