]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed bug in :meth:`.Session.bulk_save_objects` where a mapped
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 8 Sep 2015 17:00:26 +0000 (13:00 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 8 Sep 2015 17:00:26 +0000 (13:00 -0400)
column that had some kind of "fetch on update" value and was not
locally present in the given object would cause an AttributeError
within the operation.
fixes #3525

doc/build/changelog/changelog_10.rst
lib/sqlalchemy/orm/persistence.py
test/orm/test_bulk.py

index d12a51350f5341a2852a85e562ab75068e8a05a6..61dec24bfceabafbdf068e7334b3f99e753c4373 100644 (file)
 .. changelog::
     :version: 1.0.9
 
+    .. change::
+        :tags: bug, orm
+        :versions: 1.1.0b1
+        :tickets: 3525
+
+        Fixed bug in :meth:`.Session.bulk_save_objects` where a mapped
+        column that had some kind of "fetch on update" value and was not
+        locally present in the given object would cause an AttributeError
+        within the operation.
+
     .. change::
         :tags: bug, sql
         :versions: 1.1.0b1
index c785a4dee8b5334d5f77424d0cc65ad12877555a..64872288ebdb26f396a65d7dd3e6fdfba46974e9 100644 (file)
@@ -672,6 +672,8 @@ def _emit_update_statements(base_mapper, uowtransaction,
                         connection, value_params in records:
                     c = cached_connections[connection].\
                         execute(statement, params)
+
+                    # TODO: why with bookkeeping=False?
                     _postfetch(
                         mapper,
                         uowtransaction,
@@ -694,6 +696,8 @@ def _emit_update_statements(base_mapper, uowtransaction,
                     execute(statement, multiparams)
 
                 rows += c.rowcount
+
+                # TODO: why with bookkeeping=False?
                 for state, state_dict, params, mapper, \
                         connection, value_params in records:
                     _postfetch(
@@ -964,6 +968,8 @@ def _postfetch(mapper, uowtransaction, table,
     after an INSERT or UPDATE statement has proceeded for that
     state."""
 
+    # TODO: bulk is never non-False, need to clean this up
+
     prefetch_cols = result.context.compiled.prefetch
     postfetch_cols = result.context.compiled.postfetch
     returning_cols = result.context.compiled.returning
@@ -996,7 +1002,7 @@ def _postfetch(mapper, uowtransaction, table,
         mapper.class_manager.dispatch.refresh_flush(
             state, uowtransaction, load_evt_attrs)
 
-    if postfetch_cols:
+    if postfetch_cols and state:
         state._expire_attributes(state.dict,
                                  [mapper._columntoproperty[c].key
                                   for c in postfetch_cols if c in
index e2a1464a61330c4377a9ddda1c67cccf043fc867..7e1b0523fad9871d8d0607830d07fe2891093f9c 100644 (file)
@@ -2,7 +2,7 @@ from sqlalchemy import testing
 from sqlalchemy.testing import eq_
 from sqlalchemy.testing.schema import Table, Column
 from sqlalchemy.testing import fixtures
-from sqlalchemy import Integer, String, ForeignKey
+from sqlalchemy import Integer, String, ForeignKey, FetchedValue
 from sqlalchemy.orm import mapper, Session
 from sqlalchemy.testing.assertsql import CompiledSQL
 from test.orm import _fixtures
@@ -156,6 +156,58 @@ class BulkInsertUpdateTest(BulkTest, _fixtures.FixtureTest):
         )
 
 
+class BulkUDPostfetchTest(BulkTest, fixtures.MappedTest):
+    @classmethod
+    def define_tables(cls, metadata):
+        Table(
+            'a', metadata,
+            Column(
+                'id', Integer,
+                primary_key=True,
+                test_needs_autoincrement=True),
+            Column('x', Integer),
+            Column('y', Integer, server_default=FetchedValue(), server_onupdate=FetchedValue()))
+
+    @classmethod
+    def setup_classes(cls):
+        class A(cls.Comparable):
+            pass
+
+    @classmethod
+    def setup_mappers(cls):
+        A = cls.classes.A
+        a = cls.tables.a
+
+        mapper(A, a)
+
+
+    def test_insert_w_fetch(self):
+        A = self.classes.A
+
+        s = Session()
+        a1 = A(x=1)
+        s.bulk_save_objects([a1])
+        s.commit()
+
+    def test_update_w_fetch(self):
+        A = self.classes.A
+
+        s = Session()
+        a1 = A(x=1, y=2)
+        s.add(a1)
+        s.commit()
+
+        eq_(a1.id, 1)  # force a load
+        a1.x = 5
+        s.expire(a1, ['y'])
+        assert 'y' not in a1.__dict__
+        s.bulk_save_objects([a1])
+        s.commit()
+
+        eq_(a1.x, 5)
+        eq_(a1.y, 2)
+
+
 class BulkInheritanceTest(BulkTest, fixtures.MappedTest):
     @classmethod
     def define_tables(cls, metadata):