From 0997e843f2cd81a7ddbfcc73d7f87a8061139976 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 8 Sep 2015 14:12:32 -0400 Subject: [PATCH] - Internal calls to "bookkeeping" functions within :meth:`.Session.bulk_save_objects` and related bulk methods have been scaled back to the extent that this functionality is not currently used, e.g. checks for column default values to be fetched after an INSERT or UPDATE statement. fixes #3526 --- doc/build/changelog/changelog_11.rst | 10 +++ lib/sqlalchemy/orm/persistence.py | 104 ++++++++++++++------------- test/orm/test_bulk.py | 5 +- 3 files changed, 69 insertions(+), 50 deletions(-) diff --git a/doc/build/changelog/changelog_11.rst b/doc/build/changelog/changelog_11.rst index 27dc8fd462..c910136fe7 100644 --- a/doc/build/changelog/changelog_11.rst +++ b/doc/build/changelog/changelog_11.rst @@ -21,6 +21,16 @@ .. changelog:: :version: 1.1.0b1 + .. change:: + :tags: bug, orm + :tickets: 3526 + + Internal calls to "bookkeeping" functions within + :meth:`.Session.bulk_save_objects` and related bulk methods have + been scaled back to the extent that this functionality is not + currently used, e.g. checks for column default values to be + fetched after an INSERT or UPDATE statement. + .. change:: :tags: feature, orm :tickets: 2677 diff --git a/lib/sqlalchemy/orm/persistence.py b/lib/sqlalchemy/orm/persistence.py index 64872288eb..d89a93dd3f 100644 --- a/lib/sqlalchemy/orm/persistence.py +++ b/lib/sqlalchemy/orm/persistence.py @@ -674,15 +674,16 @@ def _emit_update_statements(base_mapper, uowtransaction, execute(statement, params) # TODO: why with bookkeeping=False? - _postfetch( - mapper, - uowtransaction, - table, - state, - state_dict, - c, - c.context.compiled_parameters[0], - value_params) + if bookkeeping: + _postfetch( + mapper, + uowtransaction, + table, + state, + state_dict, + c, + c.context.compiled_parameters[0], + value_params) rows += c.rowcount else: multiparams = [rec[2] for rec in records] @@ -697,18 +698,18 @@ def _emit_update_statements(base_mapper, uowtransaction, rows += c.rowcount - # TODO: why with bookkeeping=False? for state, state_dict, params, mapper, \ connection, value_params in records: - _postfetch( - mapper, - uowtransaction, - table, - state, - state_dict, - c, - c.context.compiled_parameters[0], - value_params) + if bookkeeping: + _postfetch( + mapper, + uowtransaction, + table, + state, + state_dict, + c, + c.context.compiled_parameters[0], + value_params) if check_rowcount: if rows != len(records): @@ -758,15 +759,18 @@ def _emit_insert_statements(base_mapper, uowtransaction, conn, value_params, has_all_pks, has_all_defaults), \ last_inserted_params in \ zip(records, c.context.compiled_parameters): - _postfetch( - mapper_rec, - uowtransaction, - table, - state, - state_dict, - c, - last_inserted_params, - value_params) + if state: + _postfetch( + mapper_rec, + uowtransaction, + table, + state, + state_dict, + c, + last_inserted_params, + value_params) + else: + _postfetch_bulk_save(mapper_rec, state_dict, table) else: if not has_all_defaults and base_mapper.eager_defaults: @@ -795,15 +799,19 @@ def _emit_insert_statements(base_mapper, uowtransaction, prop = mapper_rec._columntoproperty[col] if state_dict.get(prop.key) is None: state_dict[prop.key] = pk - _postfetch( - mapper_rec, - uowtransaction, - table, - state, - state_dict, - result, - result.context.compiled_parameters[0], - value_params) + if bookkeeping: + if state: + _postfetch( + mapper_rec, + uowtransaction, + table, + state, + state_dict, + result, + result.context.compiled_parameters[0], + value_params) + else: + _postfetch_bulk_save(mapper_rec, state_dict, table) def _emit_post_update_statements(base_mapper, uowtransaction, @@ -963,13 +971,11 @@ def _finalize_insert_update_commands(base_mapper, uowtransaction, states): def _postfetch(mapper, uowtransaction, table, - state, dict_, result, params, value_params, bulk=False): + state, dict_, result, params, value_params): """Expire attributes in need of newly persisted database state, 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 @@ -1002,7 +1008,7 @@ def _postfetch(mapper, uowtransaction, table, mapper.class_manager.dispatch.refresh_flush( state, uowtransaction, load_evt_attrs) - if postfetch_cols and state: + if postfetch_cols: state._expire_attributes(state.dict, [mapper._columntoproperty[c].key for c in postfetch_cols if c in @@ -1013,13 +1019,15 @@ def _postfetch(mapper, uowtransaction, table, # TODO: this still goes a little too often. would be nice to # have definitive list of "columns that changed" here for m, equated_pairs in mapper._table_to_equated[table]: - if state is None: - sync.bulk_populate_inherit_keys(dict_, m, equated_pairs) - else: - sync.populate(state, m, state, m, - equated_pairs, - uowtransaction, - mapper.passive_updates) + sync.populate(state, m, state, m, + equated_pairs, + uowtransaction, + mapper.passive_updates) + + +def _postfetch_bulk_save(mapper, dict_, table): + for m, equated_pairs in mapper._table_to_equated[table]: + sync.bulk_populate_inherit_keys(dict_, m, equated_pairs) def _connections_for_states(base_mapper, uowtransaction, states): diff --git a/test/orm/test_bulk.py b/test/orm/test_bulk.py index 7e1b0523fa..878560cf60 100644 --- a/test/orm/test_bulk.py +++ b/test/orm/test_bulk.py @@ -166,7 +166,9 @@ class BulkUDPostfetchTest(BulkTest, fixtures.MappedTest): primary_key=True, test_needs_autoincrement=True), Column('x', Integer), - Column('y', Integer, server_default=FetchedValue(), server_onupdate=FetchedValue())) + Column('y', Integer, + server_default=FetchedValue(), + server_onupdate=FetchedValue())) @classmethod def setup_classes(cls): @@ -180,7 +182,6 @@ class BulkUDPostfetchTest(BulkTest, fixtures.MappedTest): mapper(A, a) - def test_insert_w_fetch(self): A = self.classes.A -- 2.47.3