]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fixed bug in session.merge() blocking dict-like collections from merging.
authorJason Kirtland <jek@discorporate.us>
Thu, 11 Feb 2010 00:43:34 +0000 (00:43 +0000)
committerJason Kirtland <jek@discorporate.us>
Thu, 11 Feb 2010 00:43:34 +0000 (00:43 +0000)
CHANGES
lib/sqlalchemy/orm/properties.py
test/aaa_profiling/test_orm.py
test/orm/test_merge.py

diff --git a/CHANGES b/CHANGES
index 855293635afa961949e141411c2deb1c0deefbd7..96237de6cdd8ae62611a4c33941564bde8974b41 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -37,6 +37,9 @@ CHANGES
    - Fixed bug in attribute history that inadvertently invoked
      __eq__ on mapped instances.
 
+   - Fixed bug in session.merge() which prevented dict-like
+     collections from merging.
+
 - sql
   - Added math negation operator support, -x.
   
index 486aa2e4b735e79cecf4785d4f4f64a56c81bf8b..8dbc6b3db7881776a93b058d05b7d331e47d2d9b 100644 (file)
@@ -645,7 +645,10 @@ class RelationProperty(StrategizedProperty):
         if self.uselist:
             instances = source_state.get_impl(self.key).\
                             get(source_state, source_dict)
-            
+            if hasattr(instances, '_sa_adapter'):
+                # convert collections to adapters to get a true iterator
+                instances = instances._sa_adapter
+
             if load:
                 # for a full merge, pre-load the destination collection,
                 # so that individual _merge of each item pulls from identity
index a95086509c8ae8b4f4be8186819d86ca1e7049db..7bdc485ba9b0779c7ca5435002eedd03a77f606c 100644 (file)
@@ -58,7 +58,7 @@ class MergeTest(_base.MappedTest):
         # down from 185 on this
         # this is a small slice of a usually bigger
         # operation so using a small variance
-        @profiling.function_call_count(87, variance=0.001)
+        @profiling.function_call_count(91, variance=0.001)
         def go():
             return sess2.merge(p1, load=False)
             
index fab074d5c5cf226db874b36d4a80ed6827b6c631..ca7c7942dae7bc2e2013f7b87d5dc6c7f38ea2e2 100644 (file)
@@ -6,6 +6,7 @@ from sqlalchemy.test import testing
 from sqlalchemy.util import OrderedSet
 from sqlalchemy.orm import mapper, relation, create_session, PropComparator, \
                             synonym, comparable_property, sessionmaker, attributes
+from sqlalchemy.orm.collections import attribute_mapped_collection
 from sqlalchemy.orm.interfaces import MapperOption
 from sqlalchemy.test.testing import eq_, ne_
 from test.orm import _base, _fixtures
@@ -245,7 +246,22 @@ class MergeTest(_fixtures.FixtureTest):
         sess.merge(u1)
         sess.flush()
         assert u1.address_id is u1.data is None
-        
+
+    @testing.resolve_artifact_names
+    def test_merge_irregular_collection(self):
+        mapper(User, users, properties={
+            'addresses': relation(
+                mapper(Address, addresses),
+                backref='user',
+                collection_class=attribute_mapped_collection('email_address')),
+            })
+        u1 = User(id=7, name='fred')
+        u1.addresses['foo@bar.com'] = Address(email_address='foo@bar.com')
+        sess = create_session()
+        sess.merge(u1)
+        sess.flush()
+        assert u1.addresses.keys() == ['foo@bar.com']
+
     @testing.resolve_artifact_names
     def test_attribute_cascade(self):
         """Merge of a persistent entity with two child persistent entities."""