From: Mike Bayer Date: Thu, 5 May 2016 21:07:40 +0000 (-0400) Subject: Repair _orm_columns() to accommodate text() X-Git-Tag: rel_1_1_0b1~53 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9bdd6f2b1f6b34a82b77849ec05811aa0279931d;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git 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 --- diff --git a/doc/build/changelog/changelog_10.rst b/doc/build/changelog/changelog_10.rst index 352f00c8da..a44b4d62bb 100644 --- a/doc/build/changelog/changelog_10.rst +++ b/doc/build/changelog/changelog_10.rst @@ -18,6 +18,14 @@ .. 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 diff --git a/lib/sqlalchemy/orm/base.py b/lib/sqlalchemy/orm/base.py index 7947cd7d7f..8d86fb24e2 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 00c2c37bac..e0367f967b 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 d79de1d965..34343d78d0 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()