summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm/attributes.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2018-12-22 19:16:50 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2018-12-28 08:40:44 -0500
commit847d1359421ebb3b4ba653ca1a9d238e62e8e8a8 (patch)
tree88f7ab56680d37ae1cdf32d2d8cb9d6b0126da67 /lib/sqlalchemy/orm/attributes.py
parent07cea66ccb74c68fa505b5fbba91984e0375993d (diff)
downloadsqlalchemy-847d1359421ebb3b4ba653ca1a9d238e62e8e8a8.tar.gz
Check collection less than two items remaining before firing scalar backref remove
Fixed long-standing issue where duplicate collection members would cause a backref to delete the association between the member and its parent object when one of the duplicates were removed, as occurs as a side effect of swapping two objects in one statement. Fixes: #1103 Change-Id: Ic12877f7bd5a4eb688091725a78410748e7fdf16
Diffstat (limited to 'lib/sqlalchemy/orm/attributes.py')
-rw-r--r--lib/sqlalchemy/orm/attributes.py20
1 files changed, 14 insertions, 6 deletions
diff --git a/lib/sqlalchemy/orm/attributes.py b/lib/sqlalchemy/orm/attributes.py
index 5ba8be439..b08c46741 100644
--- a/lib/sqlalchemy/orm/attributes.py
+++ b/lib/sqlalchemy/orm/attributes.py
@@ -1273,19 +1273,27 @@ def backref_listeners(attribute, key, uselist):
if not child_impl.collection and not child_impl.dynamic:
check_remove_token = child_impl._remove_token
check_replace_token = child_impl._replace_token
+ check_for_dupes_on_remove = uselist and not parent_impl.dynamic
else:
check_remove_token = child_impl._remove_token
check_replace_token = child_impl._bulk_replace_token \
if child_impl.collection else None
+ check_for_dupes_on_remove = False
if initiator is not check_remove_token and \
initiator is not check_replace_token:
- child_impl.pop(
- child_state,
- child_dict,
- state.obj(),
- initiator,
- passive=PASSIVE_NO_FETCH)
+
+ if not check_for_dupes_on_remove or \
+ not util.has_dupes(
+ # when this event is called, the item is usually
+ # present in the list, except for a pop() operation.
+ state.dict[parent_impl.key], child):
+ child_impl.pop(
+ child_state,
+ child_dict,
+ state.obj(),
+ initiator,
+ passive=PASSIVE_NO_FETCH)
if uselist:
event.listen(attribute, "append",