summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy
diff options
context:
space:
mode:
authorJason Kirtland <jek@discorporate.us>2007-10-31 09:21:22 +0000
committerJason Kirtland <jek@discorporate.us>2007-10-31 09:21:22 +0000
commit2d3f907ac0a23d410ecc3c74afc6d63bd2abc186 (patch)
tree2a1f7b5b7152915d91c26087f5fdcf12dda56017 /lib/sqlalchemy
parent661774055c58d096faf929f34abd947fb5931788 (diff)
downloadsqlalchemy-2d3f907ac0a23d410ecc3c74afc6d63bd2abc186.tar.gz
Fixed a truncation error when re-assigning a subset of a collection
(obj.relation = obj.relation[1:]) [ticket:834]
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r--lib/sqlalchemy/orm/attributes.py20
1 files changed, 15 insertions, 5 deletions
diff --git a/lib/sqlalchemy/orm/attributes.py b/lib/sqlalchemy/orm/attributes.py
index 8d035d568..6d9c092a6 100644
--- a/lib/sqlalchemy/orm/attributes.py
+++ b/lib/sqlalchemy/orm/attributes.py
@@ -453,15 +453,25 @@ class CollectionAttributeImpl(AttributeImpl):
old_collection = self.get_collection(state, old)
new_collection, user_data = self._build_collection(state)
- self._load_collection(state, value or [], emit_events=True,
- collection=new_collection)
+
+ idset = util.IdentitySet
+ constants = idset(old_collection or []).intersection(value or [])
+ additions = idset(value or []).difference(constants)
+ removals = idset(old_collection or []).difference(constants)
+
+ for member in value or []:
+ if member in additions:
+ new_collection.append_with_event(member)
+ elif member in constants:
+ new_collection.append_without_event(member)
state.dict[self.key] = user_data
state.modified = True
- # mark all the old elements as detached from the parent
+ # mark all the orphaned elements as detached from the parent
if old_collection:
- old_collection.clear_with_event()
+ for member in removals:
+ old_collection.remove_with_event(member)
old_collection.unlink(old)
def set_committed_value(self, state, value):
@@ -494,7 +504,7 @@ class CollectionAttributeImpl(AttributeImpl):
else:
for item in values:
collection.append_without_event(item)
-
+
def get_collection(self, state, user_data=None):
if user_data is None:
user_data = self.get(state)