diff options
author | mike bayer <mike_mp@zzzcomputing.com> | 2023-04-21 15:28:43 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@bbpush.zzzcomputing.com> | 2023-04-21 15:28:43 +0000 |
commit | 95628d9707cdfbfdd229b2acee02fbadfbe7ced0 (patch) | |
tree | c8b17287a05a074f2c0081772b829b2f6c046140 /lib/sqlalchemy | |
parent | 98b77c36ed90894a4f7d4b9b43b8903675f36717 (diff) | |
parent | 63f51491c5f0cb22883c800a065d7c4b4c54774e (diff) | |
download | sqlalchemy-95628d9707cdfbfdd229b2acee02fbadfbe7ced0.tar.gz |
Merge "try to omit unnecessary cols for ORM bulk insert + returning" into main
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r-- | lib/sqlalchemy/orm/bulk_persistence.py | 20 | ||||
-rw-r--r-- | lib/sqlalchemy/orm/persistence.py | 16 |
2 files changed, 27 insertions, 9 deletions
diff --git a/lib/sqlalchemy/orm/bulk_persistence.py b/lib/sqlalchemy/orm/bulk_persistence.py index f9d9d6a43..c096dc3e5 100644 --- a/lib/sqlalchemy/orm/bulk_persistence.py +++ b/lib/sqlalchemy/orm/bulk_persistence.py @@ -464,8 +464,9 @@ class ORMDMLState(AbstractORMCompileState): compiler, orm_level_statement, dml_level_statement, + dml_mapper, + *, use_supplemental_cols=True, - dml_mapper=None, ): """establish ORM column handlers for an INSERT, UPDATE, or DELETE which uses explicit returning(). @@ -504,7 +505,17 @@ class ORMDMLState(AbstractORMCompileState): if use_supplemental_cols: dml_level_statement = dml_level_statement.return_defaults( - supplemental_cols=cols_to_return + # this is a little weird looking, but by passing + # primary key as the main list of cols, this tells + # return_defaults to omit server-default cols. Since + # we have cols_to_return, just return what we asked for + # (plus primary key, which ORM persistence needs since + # we likely set bookkeeping=True here, which is another + # whole thing...). We dont want to clutter the + # statement up with lots of other cols the user didn't + # ask for. see #9685 + *dml_mapper.primary_key, + supplemental_cols=cols_to_return, ) else: dml_level_statement = dml_level_statement.returning( @@ -1280,6 +1291,7 @@ class BulkORMInsert(ORMDMLState, InsertDMLState): compiler, orm_level_statement, statement, + dml_mapper=mapper, use_supplemental_cols=False, ) self.statement = statement @@ -1314,8 +1326,8 @@ class BulkORMInsert(ORMDMLState, InsertDMLState): compiler, orm_level_statement, statement, - use_supplemental_cols=True, dml_mapper=emit_insert_mapper, + use_supplemental_cols=True, ) self.statement = statement @@ -1425,6 +1437,7 @@ class BulkORMUpdate(BulkUDCompileState, UpdateDMLState): compiler, orm_level_statement, new_stmt, + dml_mapper=mapper, use_supplemental_cols=use_supplemental_cols, ) @@ -1795,6 +1808,7 @@ class BulkORMDelete(BulkUDCompileState, DeleteDMLState): compiler, orm_level_statement, new_stmt, + dml_mapper=mapper, use_supplemental_cols=use_supplemental_cols, ) diff --git a/lib/sqlalchemy/orm/persistence.py b/lib/sqlalchemy/orm/persistence.py index a331d4ed8..a12156eb5 100644 --- a/lib/sqlalchemy/orm/persistence.py +++ b/lib/sqlalchemy/orm/persistence.py @@ -1076,12 +1076,16 @@ def _emit_insert_statements( else: do_executemany = False - if not has_all_defaults and base_mapper._prefer_eager_defaults( - connection.dialect, table - ): - statement = statement.return_defaults( - *mapper._server_default_cols[table] - ) + if use_orm_insert_stmt is None: + if ( + not has_all_defaults + and base_mapper._prefer_eager_defaults( + connection.dialect, table + ) + ): + statement = statement.return_defaults( + *mapper._server_default_cols[table] + ) if mapper.version_id_col is not None: statement = statement.return_defaults(mapper.version_id_col) |