summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm/context.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2023-02-10 08:39:21 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2023-02-10 10:42:11 -0500
commiteb0861e8e69f8ce702301c558e552e1aeb2e9eba (patch)
tree638c8a08641e524b3ed54ee824275e5eb520ecbe /lib/sqlalchemy/orm/context.py
parent48aad8b244737ad7d2000056ce7320d6b32fa2de (diff)
downloadsqlalchemy-eb0861e8e69f8ce702301c558e552e1aeb2e9eba.tar.gz
generalize adapt_on_names to expect non-named elements
The fix in #9217 opened up adapt_on_names to more kinds of expressions than it was prepared for; adjust that logic and also refine in the ORM where we are using it, as we dont need it (yet) for the DML RETURNING use case. Fixed regression introduced in version 2.0.2 due to :ticket:`9217` where using DML RETURNING statements, as well as :meth:`_sql.Select.from_statement` constructs as was "fixed" in :ticket:`9217`, in conjunction with ORM mapped classes that used expressions such as with :func:`_orm.column_property`, would lead to an internal error within Core where it would attempt to match the expression by name. The fix repairs the Core issue, and also adjusts the fix in :ticket:`9217` to not take effect for the DML RETURNING use case, where it adds unnecessary overhead. Fixes: #9273 Change-Id: Ie0344efb12ff7df48f21e71e62dc598c76a6a0de
Diffstat (limited to 'lib/sqlalchemy/orm/context.py')
-rw-r--r--lib/sqlalchemy/orm/context.py14
1 files changed, 10 insertions, 4 deletions
diff --git a/lib/sqlalchemy/orm/context.py b/lib/sqlalchemy/orm/context.py
index 0e631e66f..e6f14daad 100644
--- a/lib/sqlalchemy/orm/context.py
+++ b/lib/sqlalchemy/orm/context.py
@@ -619,6 +619,8 @@ class ORMFromStatementCompileState(ORMCompileState):
**kw: Any,
) -> ORMFromStatementCompileState:
+ assert isinstance(statement_container, FromStatement)
+
if compiler is not None:
toplevel = not compiler.stack
else:
@@ -731,13 +733,13 @@ class ORMFromStatementCompileState(ORMCompileState):
# those columns completely, don't interfere with the compiler
# at all; just in ORM land, use an adapter to convert from
# our ORM columns to whatever columns are in the statement,
- # before we look in the result row. Always adapt on names
- # to accept cases such as issue #9217.
-
+ # before we look in the result row. Adapt on names
+ # to accept cases such as issue #9217, however also allow
+ # this to be overridden for cases such as #9273.
self._from_obj_alias = ORMStatementAdapter(
_TraceAdaptRole.ADAPT_FROM_STATEMENT,
self.statement,
- adapt_on_names=True,
+ adapt_on_names=statement_container._adapt_on_names,
)
return self
@@ -781,6 +783,8 @@ class FromStatement(GroupedElement, Generative, TypedReturnsRows[_TP]):
element: Union[ExecutableReturnsRows, TextClause]
+ _adapt_on_names: bool
+
_traverse_internals = [
("_raw_columns", InternalTraversal.dp_clauseelement_list),
("element", InternalTraversal.dp_clauseelement),
@@ -794,6 +798,7 @@ class FromStatement(GroupedElement, Generative, TypedReturnsRows[_TP]):
self,
entities: Iterable[_ColumnsClauseArgument[Any]],
element: Union[ExecutableReturnsRows, TextClause],
+ _adapt_on_names: bool = True,
):
self._raw_columns = [
coercions.expect(
@@ -809,6 +814,7 @@ class FromStatement(GroupedElement, Generative, TypedReturnsRows[_TP]):
self._label_style = (
element._label_style if is_select_base(element) else None
)
+ self._adapt_on_names = _adapt_on_names
def _compiler_dispatch(self, compiler, **kw):