From d0873ec7735f8238d74b860d6a8a85d55b2dbd1d Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 18 Feb 2025 10:20:32 -0500 Subject: [PATCH] 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 --- doc/build/changelog/unreleased_20/12357.rst | 9 +++++++++ lib/sqlalchemy/sql/elements.py | 4 ++++ test/orm/test_core_compilation.py | 8 ++++++++ 3 files changed, 21 insertions(+) create mode 100644 doc/build/changelog/unreleased_20/12357.rst 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 57a3187015..825123a977 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -2987,6 +2987,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()), -- 2.47.2