]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Repair _orm_columns() to accommodate text()
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 5 May 2016 21:07:40 +0000 (17:07 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 5 May 2016 21:08:31 +0000 (17:08 -0400)
Fixed bug whereby passing a :func:`.text` construct to the
:meth:`.Query.group_by` method would raise an error, instead
of intepreting the object as a SQL fragment.

Change-Id: I5fc2f590b76508d52e23b5fa9cf037ddea8080c3
fixes: #3706
(cherry picked from commit 9bdd6f2b1f6b34a82b77849ec05811aa0279931d)

doc/build/changelog/changelog_10.rst
lib/sqlalchemy/orm/base.py
lib/sqlalchemy/sql/elements.py
test/orm/test_query.py

index 352f00c8da6ab1a0726c3e2e337b5bab6d7221b5..a44b4d62bb2016d3d9cdf383c252eb46399fc826 100644 (file)
 .. changelog::
     :version: 1.0.13
 
+    .. change::
+        :tags: bug, orm
+        :tickets: 3706
+
+        Fixed bug whereby passing a :func:`.text` construct to the
+        :meth:`.Query.group_by` method would raise an error, instead
+        of intepreting the object as a SQL fragment.
+
     .. change::
         :tags: bug, oracle
         :tickets: 3699
index 7947cd7d7fbf4b1e274716ab8443429cd36ae33f..8d86fb24e20ff284b87bb3772113fa3ff24c1484 100644 (file)
@@ -344,7 +344,7 @@ def _attr_as_key(attr):
 
 def _orm_columns(entity):
     insp = inspection.inspect(entity, False)
-    if hasattr(insp, 'selectable'):
+    if hasattr(insp, 'selectable') and hasattr(insp.selectable, 'c'):
         return [c for c in insp.selectable.c]
     else:
         return [entity]
index 124dbdb98c93c7fe033902cc5d5ad658b6921043..c564777b45d1ac7da404a17d1dcb0bd7b27d99b0 100644 (file)
@@ -1266,6 +1266,8 @@ class TextClause(Executable, ClauseElement):
 
     @property
     def selectable(self):
+        # allows text() to be considered by
+        # _interpret_as_from
         return self
 
     _hide_froms = []
index 37ee67620da328a9621487c926d599e5b6632137..19fd016bc05b3c9acbfc96c3d8f141c72d3149f6 100644 (file)
@@ -2978,6 +2978,25 @@ class TextTest(QueryTest, AssertsCompiledSQL):
             [User(id=7), User(id=8), User(id=9), User(id=10)]
         )
 
+    def test_group_by_accepts_text(self):
+        User = self.classes.User
+        s = create_session()
+
+        q = s.query(User).group_by(text("name"))
+        self.assert_compile(
+            q,
+            "SELECT users.id AS users_id, users.name AS users_name "
+            "FROM users GROUP BY name"
+        )
+
+    def test_orm_columns_accepts_text(self):
+        from sqlalchemy.orm.base import _orm_columns
+        t = text("x")
+        eq_(
+            _orm_columns(t),
+            [t]
+        )
+
     def test_order_by_w_eager_one(self):
         User = self.classes.User
         s = create_session()