From 332f5ee2662835ed1ca008043d40c37d7cddc270 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 25 Nov 2008 04:43:04 +0000 Subject: - Duplicate items in a list-based collection will be maintained when issuing INSERTs to a "secondary" table in a many-to-many relation. Assuming the m2m table has a unique or primary key constraint on it, this will raise the expected constraint violation instead of silently dropping the duplicate entries. Note that the old behavior remains for a one-to-many relation since collection entries in that case don't result in INSERT statements and SQLA doesn't manually police collections. [ticket:1232] --- lib/sqlalchemy/orm/attributes.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/sqlalchemy/orm/attributes.py b/lib/sqlalchemy/orm/attributes.py index 662ea05d3..79be76c3a 100644 --- a/lib/sqlalchemy/orm/attributes.py +++ b/lib/sqlalchemy/orm/attributes.py @@ -1427,11 +1427,15 @@ class History(tuple): elif original is NEVER_SET: return cls((), list(current), ()) else: - collection = util.OrderedIdentitySet(current) - s = util.OrderedIdentitySet(original) - return cls(list(collection.difference(s)), - list(collection.intersection(s)), - list(s.difference(collection))) + current_set = util.IdentitySet(current) + original_set = util.IdentitySet(original) + + # ensure duplicates are maintained + return cls( + [x for x in current if x not in original_set], + [x for x in current if x in original_set], + [x for x in original if x not in current_set] + ) else: if current is NO_VALUE: if original not in [None, NEVER_SET, NO_VALUE]: -- cgit v1.2.1