]> 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:07:40 +0000 (17:07 -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

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 00c2c37bacf09155bd9d5b8b351e2001c0a9be8b..e0367f967b416dfc93ffc6550c3233fdde528b52 100644 (file)
@@ -1206,6 +1206,8 @@ class TextClause(Executable, ClauseElement):
 
     @property
     def selectable(self):
+        # allows text() to be considered by
+        # _interpret_as_from
         return self
 
     _hide_froms = []
index d79de1d9654d133e67a3ff42e3ba3e986c2c7241..34343d78d072b538d4c5be3400d872e615921417 100644 (file)
@@ -3248,6 +3248,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()