Commits

Mike Bayer  committed da5b71e

some rudimentary fixes to get instance-level deferreds/lazy loads to transfer over on merge()

  • Participants
  • Parent commits 37d51c1

Comments (0)

Files changed (2)

File lib/sqlalchemy/orm/properties.py

         getattr(state.class_, self.key).impl.set(state, value, None)
         
     def merge(self, session, source, dest, dont_load, _recursive):
-        setattr(dest, self.key, getattr(source, self.key, None))
-
+        value = attributes.get_as_list(source._state, self.key, passive=True)
+        if value:
+            setattr(dest, self.key, value[0])
+        else:
+            # TODO: lazy callable should merge to the new instance
+            dest._state.expire_attributes([self.key])
+            
     def get_col_value(self, column, value):
         return value
 
 
     def merge(self, session, source, dest, dont_load, _recursive):
         if not "merge" in self.cascade:
+            # TODO: lazy callable should merge to the new instance
+            dest._state.expire_attributes([self.key])
             return
         instances = attributes.get_as_list(source._state, self.key, passive=True)
         if not instances:

File test/orm/pickled.py

         sess2.update(u2)
         self.assertEquals(u2.name, 'ed')
         self.assertEquals(u2, User(name='ed', addresses=[Address(email_address='ed@bar.com')]))
+
+        u2 = pickle.loads(pickle.dumps(u1))
+        sess2 = create_session()
+        u2 = sess2.merge(u2, dont_load=True)
+        self.assertEquals(u2.name, 'ed')
+        self.assertEquals(u2, User(name='ed', addresses=[Address(email_address='ed@bar.com')]))
         
     def test_instance_deferred_cols(self):
         mapper(User, users, properties={
         sess2 = create_session()
         sess2.update(u2)
         self.assertEquals(u2.name, 'ed')
-        assert 'addresses' not in u1.__dict__
+        assert 'addresses' not in u2.__dict__
         ad = u2.addresses[0]
         assert 'email_address' not in ad.__dict__
         self.assertEquals(ad.email_address, 'ed@bar.com')
         self.assertEquals(u2, User(name='ed', addresses=[Address(email_address='ed@bar.com')]))
 
+        u2 = pickle.loads(pickle.dumps(u1))
+        sess2 = create_session()
+        u2 = sess2.merge(u2, dont_load=True)
+        self.assertEquals(u2.name, 'ed')
+        assert 'addresses' not in u2.__dict__
+        ad = u2.addresses[0]
+        assert 'email_address' in ad.__dict__  # mapper options dont transmit over merge() right now
+        self.assertEquals(ad.email_address, 'ed@bar.com')
+        self.assertEquals(u2, User(name='ed', addresses=[Address(email_address='ed@bar.com')]))
+
+
 class PolymorphicDeferredTest(ORMTest):
     def define_tables(self, metadata):
         global users, email_users