From dc60e7a7d35a470c09ce590f37e949ff8e8cdcde Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 5 May 2023 09:16:10 -0400 Subject: add explicit step to set populate_existing for bulk insert Fixed issue in new :ref:`orm_queryguide_upsert_returning` feature where the ``populate_existing`` execution option was not being propagated to the loading option, preventing existing attributes from being refreshed in-place. Fixes: #9746 Change-Id: I3efcab644e2b5874c6b265d5313f353c051db629 --- lib/sqlalchemy/orm/bulk_persistence.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'lib/sqlalchemy') diff --git a/lib/sqlalchemy/orm/bulk_persistence.py b/lib/sqlalchemy/orm/bulk_persistence.py index cb416d69e..257d71db4 100644 --- a/lib/sqlalchemy/orm/bulk_persistence.py +++ b/lib/sqlalchemy/orm/bulk_persistence.py @@ -586,6 +586,7 @@ class ORMDMLState(AbstractORMCompileState): load_options = execution_options.get( "_sa_orm_load_options", QueryContext.default_load_options ) + querycontext = QueryContext( compile_state.from_statement_ctx, compile_state.select_statement, @@ -1140,6 +1141,7 @@ class BulkORMInsert(ORMDMLState, InsertDMLState): _return_defaults: bool = False _subject_mapper: Optional[Mapper[Any]] = None _autoflush: bool = True + _populate_existing: bool = False select_statement: Optional[FromStatement] = None @@ -1159,7 +1161,7 @@ class BulkORMInsert(ORMDMLState, InsertDMLState): execution_options, ) = BulkORMInsert.default_insert_options.from_execution_options( "_sa_orm_insert_options", - {"dml_strategy", "autoflush"}, + {"dml_strategy", "autoflush", "populate_existing"}, execution_options, statement._execution_options, ) @@ -1284,6 +1286,15 @@ class BulkORMInsert(ORMDMLState, InsertDMLState): if not bool(statement._returning): return result + if insert_options._populate_existing: + load_options = execution_options.get( + "_sa_orm_load_options", QueryContext.default_load_options + ) + load_options += {"_populate_existing": True} + execution_options = execution_options.union( + {"_sa_orm_load_options": load_options} + ) + return cls._return_orm_returning( session, statement, -- cgit v1.2.1