From c1c999c01d595b74fe178d9bdbff34fd8939a283 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 18 Mar 2021 19:34:32 -0400 Subject: Ensure ClauseAdapter treats FunctionElement as a ColumnElement Fixed regression where use of an unnamed SQL expression such as a SQL function would raise a column targeting error if the query itself were using joinedload for an entity and was also being wrapped in a subquery by the joinedload eager loading process. Fixes: #6086 Change-Id: I22cf4d6974685267c4f903bd7639be8271c6c1ef --- lib/sqlalchemy/orm/context.py | 2 ++ lib/sqlalchemy/sql/util.py | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) (limited to 'lib/sqlalchemy') diff --git a/lib/sqlalchemy/orm/context.py b/lib/sqlalchemy/orm/context.py index 9cfa0fdc6..e440ee161 100644 --- a/lib/sqlalchemy/orm/context.py +++ b/lib/sqlalchemy/orm/context.py @@ -2707,7 +2707,9 @@ class _ORMColumnEntity(_ColumnEntity): compile_state._entities.append(self) compile_state._has_orm_entities = True + self.column = column + self._fetch_column = self._row_processor = None self._extra_entities = (self.expr, self.column) diff --git a/lib/sqlalchemy/sql/util.py b/lib/sqlalchemy/sql/util.py index 5f35de5f0..4300d8a29 100644 --- a/lib/sqlalchemy/sql/util.py +++ b/lib/sqlalchemy/sql/util.py @@ -843,8 +843,13 @@ class ClauseAdapter(visitors.ReplacingExternalTraversal): newcol = self.selectable.exported_columns.get(col.name) return newcol + @util.preload_module("sqlalchemy.sql.functions") def replace(self, col): - if isinstance(col, FromClause): + functions = util.preloaded.sql_functions + + if isinstance(col, FromClause) and not isinstance( + col, functions.FunctionElement + ): if self.selectable.is_derived_from(col): return self.selectable elif isinstance(col, Alias) and isinstance( -- cgit v1.2.1