From: Mike Bayer Date: Tue, 18 Feb 2025 15:20:32 +0000 (-0500) Subject: apply _propagate_attrs in _construct_for_list X-Git-Tag: rel_2_0_39~16^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1e2e25c03d60e723d6de37366580f644e2c42c73;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git apply _propagate_attrs in _construct_for_list Fixed issue where the "is ORM" flag of a :func:`.select` or other ORM statement would not be propagated to the ORM :class:`.Session` based on a multi-part operator expression alone, e.g. such as ``Cls.attr + Cls.attr + Cls.attr`` or similar, leading to ORM behaviors not taking place for such statements. Fixes: #12357 Change-Id: I61130eeb3c7a32c1830731fd9ad4eb99a64abf7d (cherry picked from commit d0873ec7735f8238d74b860d6a8a85d55b2dbd1d) --- diff --git a/doc/build/changelog/unreleased_20/12357.rst b/doc/build/changelog/unreleased_20/12357.rst new file mode 100644 index 0000000000..79fd888ba3 --- /dev/null +++ b/doc/build/changelog/unreleased_20/12357.rst @@ -0,0 +1,9 @@ +.. change:: + :tags: bug, orm + :tickets: 12357 + + Fixed issue where the "is ORM" flag of a :func:`.select` or other ORM + statement would not be propagated to the ORM :class:`.Session` based on a + multi-part operator expression alone, e.g. such as ``Cls.attr + Cls.attr + + Cls.attr`` or similar, leading to ORM behaviors not taking place for such + statements. diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index 6f20d7efa0..fde503aaf9 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -2977,6 +2977,10 @@ class ExpressionClauseList(OperatorExpression[_T]): self.clauses = clauses self.operator = operator self.type = type_ + for c in clauses: + if c._propagate_attrs: + self._propagate_attrs = c._propagate_attrs + break return self def _negate(self) -> Any: diff --git a/test/orm/test_core_compilation.py b/test/orm/test_core_compilation.py index 6af9185836..a961962d91 100644 --- a/test/orm/test_core_compilation.py +++ b/test/orm/test_core_compilation.py @@ -368,6 +368,14 @@ class PropagateAttrsTest(QueryTest): def propagate_cases(): return testing.combinations( (lambda: select(1), False), + (lambda User: select(User.id), True), + (lambda User: select(User.id + User.id), True), + (lambda User: select(User.id + User.id + User.id), True), + (lambda User: select(sum([User.id] * 10, User.id)), True), # type: ignore # noqa: E501 + ( + lambda User: select(literal_column("3") + User.id + User.id), + True, + ), (lambda User: select(func.count(User.id)), True), ( lambda User: select(1).select_from(select(User).subquery()),