]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Restore Query.selectable
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 18 Mar 2021 14:27:28 +0000 (10:27 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 18 Mar 2021 14:27:28 +0000 (10:27 -0400)
Fixes: #6088
Change-Id: Id014fbd081c0659d1939d059779780798cc8c1dd

doc/build/changelog/unreleased_14/6088.rst [new file with mode: 0644]
lib/sqlalchemy/orm/query.py
test/orm/test_query.py

diff --git a/doc/build/changelog/unreleased_14/6088.rst b/doc/build/changelog/unreleased_14/6088.rst
new file mode 100644 (file)
index 0000000..85c9853
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+    :tags: bug, regression, orm
+    :tickets: 6088
+
+    Fixed regression where the :attr:`_orm.Query.selectable` accessor, which is
+    a synonym for :meth:`_orm.Query.__clause_element__`, got removed, it's now
+    restored.
index 6a8a2c5713e24eb196ed83085d90c13e8a6383ea..96b4c562443d2442daf135b4d9cb8a3d3e7b8973 100644 (file)
@@ -595,6 +595,18 @@ class Query(
 
         return self.enable_eagerloads(False).statement.scalar_subquery()
 
+    @property
+    def selectable(self):
+        """Return the :class:`_expression.Select` object emitted by this
+        :class:`_query.Query`.
+
+        Used for :func:`_sa.inspect` compatibility, this is equivalent to::
+
+            query.enable_eagerloads(False).with_labels().statement
+
+        """
+        return self.__clause_element__()
+
     def __clause_element__(self):
         return (
             self.enable_eagerloads(False)
index 800c0ad5df833fb820d1ecfc2727cb0374b75914..0613cfe06c8f61d674ee513cd35598fee07ff516 100644 (file)
@@ -104,6 +104,35 @@ class MiscTest(QueryTest):
         assert q2.session is s2
         assert q1.session is s1
 
+    @testing.combinations(
+        (lambda s, User: s.query(User)),
+        (lambda s, User: s.query(User).filter_by(name="x")),
+        (lambda s, User: s.query(User.id, User.name).filter_by(name="x")),
+        (
+            lambda s, User: s.query(func.count(User.id)).filter(
+                User.name == "x"
+            )
+        ),
+    )
+    def test_rudimentary_statement_accessors(self, test_case):
+        User = self.classes.User
+
+        s = fixture_session()
+
+        q1 = testing.resolve_lambda(test_case, s=s, User=User)
+
+        is_true(
+            q1.statement.set_label_style(
+                LABEL_STYLE_TABLENAME_PLUS_COL
+            ).compare(q1.__clause_element__())
+        )
+
+        is_true(
+            q1.statement.set_label_style(
+                LABEL_STYLE_TABLENAME_PLUS_COL
+            ).compare(q1.selectable)
+        )
+
 
 class OnlyReturnTuplesTest(QueryTest):
     def test_single_entity_false(self):