From 23a1c60982dc4799c76f0cec276a3bae8a24395b Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 28 Apr 2019 12:40:31 -0400 Subject: Warn on merge of already-pending object A warning is now emitted for the case where a transient object is being merged into the session with :meth:`.Session.merge` when that object is already transient in the :class:`.Session`. This warns for the case where the object would normally be double-inserted. Fixes: #4647 Change-Id: Ie5223a59a2856664bf283017e962caf8c4230536 --- lib/sqlalchemy/orm/session.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'lib/sqlalchemy') diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py index 0b1a3b101..eccd60fe2 100644 --- a/lib/sqlalchemy/orm/session.py +++ b/lib/sqlalchemy/orm/session.py @@ -2110,6 +2110,13 @@ class Session(_SessionClassMethods): key = state.key if key is None: + if state in self._new: + util.warn( + "Instance %s is already pending in this Session yet is " + "being merged again; this is probably not what you want " + "to do" % state_str(state) + ) + if not load: raise sa_exc.InvalidRequestError( "merge() with load=False option does not support " -- cgit v1.2.1