From 4aa43ecbd78e5a7dd3d983ca46a377af4e01877e Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 4 Oct 2019 11:12:27 -0400 Subject: Warn for object replaced in identity map during flush A warning is emitted for a condition in which the :class:`.Session` may implicitly swap an object out of the identity map for another one with the same primary key, detaching the old one, which can be an observed result of load operations which occur within the :meth:`.SessionEvents.after_flush` hook. The warning is intended to notify the user that some special condition has caused this to happen and that the previous object may not be in the expected state. Fixes: #4890 Change-Id: Ide11c6b9f21ca67ff5a96266c521d0c56fd6af8d --- lib/sqlalchemy/orm/identity.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'lib/sqlalchemy/orm/identity.py') diff --git a/lib/sqlalchemy/orm/identity.py b/lib/sqlalchemy/orm/identity.py index 2da5d66b3..842b7915a 100644 --- a/lib/sqlalchemy/orm/identity.py +++ b/lib/sqlalchemy/orm/identity.py @@ -125,10 +125,13 @@ class WeakInstanceDict(IdentityMap): if existing is not state: self._manage_removed_state(existing) else: - return + return None + else: + existing = None self._dict[state.key] = state self._manage_incoming_state(state) + return existing def add(self, state): key = state.key @@ -297,9 +300,12 @@ class StrongInstanceDict(IdentityMap): self._manage_removed_state(existing) else: return + else: + existing = None self._dict[state.key] = state.obj() self._manage_incoming_state(state) + return existing def add(self, state): if state.key in self: -- cgit v1.2.1