From e3710b405ff2680bf67c0eb0cf91182f9094237d Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 25 Nov 2022 16:49:28 -0500 Subject: improve column targeting issues with query_expression Fixed issues in :func:`_orm.with_expression` where expressions that were composed of columns within a subquery being SELECTed from, or when using ``.from_statement()``, would not render correct SQL **if** the expression had a label name that matched the attribute which used :func:`_orm.query_expression`, even when :func:`_orm.query_expression` had no default expression. For the moment, if the :func:`_orm.query_expression` **does** have a default expression, that label name is still used for that default, and an additional label with the same name will be ignored. Overall, this case is pretty thorny so further adjustments might be warranted. Fixes: #8881 Change-Id: Ie939b1470cb2e824717384be65f4cd8edd619942 --- lib/sqlalchemy/orm/_orm_constructors.py | 1 + lib/sqlalchemy/orm/properties.py | 3 +++ 2 files changed, 4 insertions(+) (limited to 'lib/sqlalchemy') diff --git a/lib/sqlalchemy/orm/_orm_constructors.py b/lib/sqlalchemy/orm/_orm_constructors.py index 30119d9d7..c4abb1c8e 100644 --- a/lib/sqlalchemy/orm/_orm_constructors.py +++ b/lib/sqlalchemy/orm/_orm_constructors.py @@ -1970,6 +1970,7 @@ def query_expression( info=info, doc=doc, ) + prop.strategy_key = (("query_expression", True),) return prop diff --git a/lib/sqlalchemy/orm/properties.py b/lib/sqlalchemy/orm/properties.py index b8e1521a2..f942ad092 100644 --- a/lib/sqlalchemy/orm/properties.py +++ b/lib/sqlalchemy/orm/properties.py @@ -221,6 +221,9 @@ class ColumnProperty( ] def _memoized_attr__renders_in_subqueries(self) -> bool: + if ("query_expression", True) in self.strategy_key: + return self.strategy._have_default_expression # type: ignore + return ("deferred", True) not in self.strategy_key or ( self not in self.parent._readonly_props # type: ignore ) -- cgit v1.2.1