summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2016-05-05 17:07:40 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2016-05-05 17:07:40 -0400
commit9bdd6f2b1f6b34a82b77849ec05811aa0279931d (patch)
treebbc05c15814ce57189a30e619a846e0ab544a430
parentf1920d2713fc43cfa7089e7bd5592908e90fd69b (diff)
downloadsqlalchemy-9bdd6f2b1f6b34a82b77849ec05811aa0279931d.tar.gz
Repair _orm_columns() to accommodate text()
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
-rw-r--r--doc/build/changelog/changelog_10.rst8
-rw-r--r--lib/sqlalchemy/orm/base.py2
-rw-r--r--lib/sqlalchemy/sql/elements.py2
-rw-r--r--test/orm/test_query.py19
4 files changed, 30 insertions, 1 deletions
diff --git a/doc/build/changelog/changelog_10.rst b/doc/build/changelog/changelog_10.rst
index 352f00c8d..a44b4d62b 100644
--- a/doc/build/changelog/changelog_10.rst
+++ b/doc/build/changelog/changelog_10.rst
@@ -19,6 +19,14 @@
: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
diff --git a/lib/sqlalchemy/orm/base.py b/lib/sqlalchemy/orm/base.py
index 7947cd7d7..8d86fb24e 100644
--- a/lib/sqlalchemy/orm/base.py
+++ b/lib/sqlalchemy/orm/base.py
@@ -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]
diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py
index 00c2c37ba..e0367f967 100644
--- a/lib/sqlalchemy/sql/elements.py
+++ b/lib/sqlalchemy/sql/elements.py
@@ -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 = []
diff --git a/test/orm/test_query.py b/test/orm/test_query.py
index d79de1d96..34343d78d 100644
--- a/test/orm/test_query.py
+++ b/test/orm/test_query.py
@@ -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()