diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-05-09 10:27:51 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-05-09 10:27:51 -0400 |
commit | 319f09ffced3f655e7d500b3a9965e19468fd9d9 (patch) | |
tree | d3e3c54b33d3d8fdc18b687e00464784cc3a7cb7 /lib/sqlalchemy/orm/context.py | |
parent | 47c91d06b56b0a0cf366d3c1f8b6d71a82149e43 (diff) | |
download | sqlalchemy-319f09ffced3f655e7d500b3a9965e19468fd9d9.tar.gz |
dont use the label convention for memoized entities
Fixed issue where ORM results would apply incorrect key names to the
returned :class:`.Row` objects in the case where the set of columns to be
selected were changed, such as when using
:meth:`.Select.with_only_columns`.
Fixes: #8001
Change-Id: If3a2a5d00d15ebc2e9d41494845cfb3b06f80dcc
Diffstat (limited to 'lib/sqlalchemy/orm/context.py')
-rw-r--r-- | lib/sqlalchemy/orm/context.py | 48 |
1 files changed, 38 insertions, 10 deletions
diff --git a/lib/sqlalchemy/orm/context.py b/lib/sqlalchemy/orm/context.py index 7a263daf8..28fea2f9b 100644 --- a/lib/sqlalchemy/orm/context.py +++ b/lib/sqlalchemy/orm/context.py @@ -767,10 +767,6 @@ class ORMSelectCompileState(ORMCompileState, SelectState): else: self.label_style = self.select_statement._label_style - self._label_convention = self._column_naming_convention( - statement._label_style, self.use_legacy_query_style - ) - if select_statement._memoized_select_entities: self._memoized_entities = { memoized_entities: _QueryEntity.to_compile_state( @@ -784,6 +780,14 @@ class ORMSelectCompileState(ORMCompileState, SelectState): ) } + # label_convention is stateful and will yield deduping keys if it + # sees the same key twice. therefore it's important that it is not + # invoked for the above "memoized" entities that aren't actually + # in the columns clause + self._label_convention = self._column_naming_convention( + statement._label_style, self.use_legacy_query_style + ) + _QueryEntity.to_compile_state( self, select_statement._raw_columns, @@ -2202,11 +2206,15 @@ class _QueryEntity: entity._select_iterable, entities_collection, idx, + is_current_entities, ) else: if entity._annotations.get("bundle", False): _BundleEntity( - compile_state, entity, entities_collection + compile_state, + entity, + entities_collection, + is_current_entities, ) elif entity._is_clause_list: # this is legacy only - test_composites.py @@ -2216,10 +2224,15 @@ class _QueryEntity: entity._select_iterable, entities_collection, idx, + is_current_entities, ) else: _ColumnEntity._for_columns( - compile_state, [entity], entities_collection, idx + compile_state, + [entity], + entities_collection, + idx, + is_current_entities, ) elif entity.is_bundle: _BundleEntity(compile_state, entity, entities_collection) @@ -2417,6 +2430,7 @@ class _BundleEntity(_QueryEntity): compile_state, expr, entities_collection, + is_current_entities, setup_entities=True, parent_bundle=None, ): @@ -2447,6 +2461,7 @@ class _BundleEntity(_QueryEntity): compile_state, expr, entities_collection, + is_current_entities, parent_bundle=self, ) elif isinstance(expr, Bundle): @@ -2454,6 +2469,7 @@ class _BundleEntity(_QueryEntity): compile_state, expr, entities_collection, + is_current_entities, parent_bundle=self, ) else: @@ -2462,6 +2478,7 @@ class _BundleEntity(_QueryEntity): [expr], entities_collection, None, + is_current_entities, parent_bundle=self, ) @@ -2527,6 +2544,7 @@ class _ColumnEntity(_QueryEntity): columns, entities_collection, raw_column_index, + is_current_entities, parent_bundle=None, ): for column in columns: @@ -2546,6 +2564,7 @@ class _ColumnEntity(_QueryEntity): entities_collection, _entity, raw_column_index, + is_current_entities, parent_bundle=parent_bundle, ) else: @@ -2555,6 +2574,7 @@ class _ColumnEntity(_QueryEntity): entities_collection, _entity, raw_column_index, + is_current_entities, parent_bundle=parent_bundle, ) else: @@ -2563,6 +2583,7 @@ class _ColumnEntity(_QueryEntity): column, entities_collection, raw_column_index, + is_current_entities, parent_bundle=parent_bundle, ) @@ -2653,12 +2674,14 @@ class _RawColumnEntity(_ColumnEntity): column, entities_collection, raw_column_index, + is_current_entities, parent_bundle=None, ): self.expr = column self.raw_column_index = raw_column_index self.translate_raw_column = raw_column_index is not None - if column._is_text_clause: + + if not is_current_entities or column._is_text_clause: self._label_name = None else: self._label_name = compile_state._label_convention(column) @@ -2717,6 +2740,7 @@ class _ORMColumnEntity(_ColumnEntity): entities_collection, parententity, raw_column_index, + is_current_entities, parent_bundle=None, ): annotations = column._annotations @@ -2743,9 +2767,13 @@ class _ORMColumnEntity(_ColumnEntity): self.translate_raw_column = raw_column_index is not None self.raw_column_index = raw_column_index - self._label_name = compile_state._label_convention( - column, col_name=orm_key - ) + + if is_current_entities: + self._label_name = compile_state._label_convention( + column, col_name=orm_key + ) + else: + self._label_name = None _entity._post_inspect self.entity_zero = self.entity_zero_or_selectable = ezero = _entity |