mirror of
https://github.com/sqlalchemy/sqlalchemy.git
synced 2026-05-30 12:34:52 -04:00
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
(cherry picked from commit 9bdd6f2b1f)
This commit is contained in:
Vendored
+8
@@ -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
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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 = []
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user