summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy
diff options
context:
space:
mode:
authormike bayer <mike_mp@zzzcomputing.com>2018-12-29 07:59:58 +0000
committerGerrit Code Review <gerrit@bbpush.zzzcomputing.com>2018-12-29 07:59:58 +0000
commitcfc49b45712c4e54187f47e03432331bae564037 (patch)
treefa1895bf498a509083eedb8d7ccbbf15a0d59563 /lib/sqlalchemy
parent010dd34e7c7707fbab81d94a7ce49120b27dd448 (diff)
parent847d1359421ebb3b4ba653ca1a9d238e62e8e8a8 (diff)
downloadsqlalchemy-cfc49b45712c4e54187f47e03432331bae564037.tar.gz
Merge "Check collection less than two items remaining before firing scalar backref remove"
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r--lib/sqlalchemy/orm/attributes.py20
-rw-r--r--lib/sqlalchemy/util/__init__.py3
-rw-r--r--lib/sqlalchemy/util/_collections.py30
3 files changed, 46 insertions, 7 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",
diff --git a/lib/sqlalchemy/util/__init__.py b/lib/sqlalchemy/util/__init__.py
index 9229d0797..d8c28d6af 100644
--- a/lib/sqlalchemy/util/__init__.py
+++ b/lib/sqlalchemy/util/__init__.py
@@ -21,7 +21,8 @@ from ._collections import KeyedTuple, ImmutableContainer, immutabledict, \
UniqueAppender, PopulateDict, EMPTY_SET, to_list, to_set, \
to_column_set, update_copy, flatten_iterator, has_intersection, \
LRUCache, ScopedRegistry, ThreadLocalRegistry, WeakSequence, \
- coerce_generator_arg, lightweight_named_tuple, collections_abc
+ coerce_generator_arg, lightweight_named_tuple, collections_abc, \
+ has_dupes
from .langhelpers import iterate_attributes, class_hierarchy, \
portable_instancemethod, unbound_method_to_callable, \
diff --git a/lib/sqlalchemy/util/_collections.py b/lib/sqlalchemy/util/_collections.py
index 3152458bf..43440134a 100644
--- a/lib/sqlalchemy/util/_collections.py
+++ b/lib/sqlalchemy/util/_collections.py
@@ -1057,3 +1057,33 @@ def _iter_id(iterable):
for item in iterable:
yield id(item), item
+
+
+def has_dupes(sequence, target):
+ """Given a sequence and search object, return True if there's more
+ than one, False if zero or one of them.
+
+
+ """
+ # compare to .index version below, this version introduces less function
+ # overhead and is usually the same speed. At 15000 items (way bigger than
+ # a relationship-bound collection in memory usually is) it begins to
+ # fall behind the other version only by microseconds.
+ c = 0
+ for item in sequence:
+ if item is target:
+ c += 1
+ if c > 1:
+ return True
+ return False
+
+# .index version. the two __contains__ calls as well
+# as .index() and isinstance() slow this down.
+# def has_dupes(sequence, target):
+# if target not in sequence:
+# return False
+# elif not isinstance(sequence, collections_abc.Sequence):
+# return False
+#
+# idx = sequence.index(target)
+# return target in sequence[idx + 1:]