From eb0861e8e69f8ce702301c558e552e1aeb2e9eba Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 10 Feb 2023 08:39:21 -0500 Subject: 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 --- lib/sqlalchemy/sql/util.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'lib/sqlalchemy/sql') diff --git a/lib/sqlalchemy/sql/util.py b/lib/sqlalchemy/sql/util.py index 1dad9ce68..0a50197a0 100644 --- a/lib/sqlalchemy/sql/util.py +++ b/lib/sqlalchemy/sql/util.py @@ -54,6 +54,7 @@ from .elements import ColumnElement from .elements import Grouping from .elements import KeyedColumnElement from .elements import Label +from .elements import NamedColumn from .elements import Null from .elements import UnaryExpression from .schema import Column @@ -712,7 +713,6 @@ class _repr_params(_repr_base): return "(%s)" % elements def _get_batches(self, params: Iterable[Any]) -> Any: - lparams = list(params) lenparams = len(lparams) if lenparams > self.max_params: @@ -1122,7 +1122,6 @@ class ClauseAdapter(visitors.ReplacingExternalTraversal): def _corresponding_column( self, col, require_embedded, _seen=util.EMPTY_SET ): - newcol = self.selectable.corresponding_column( col, require_embedded=require_embedded ) @@ -1135,7 +1134,12 @@ class ClauseAdapter(visitors.ReplacingExternalTraversal): ) if newcol is not None: return newcol - if self.adapt_on_names and newcol is None: + + if ( + self.adapt_on_names + and newcol is None + and isinstance(col, NamedColumn) + ): newcol = self.selectable.exported_columns.get(col.name) return newcol -- cgit v1.2.1