From: Mike Bayer Date: Sun, 8 Mar 2009 19:06:12 +0000 (+0000) Subject: - Query.group_by() properly takes into account aliasing applied X-Git-Tag: rel_0_5_3~12 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=de06f512db3a6500e8c1b0aa4f458873155cdf0b;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Query.group_by() properly takes into account aliasing applied to the FROM clause, such as with select_from(), using with_polymorphic(), or using from_self(). --- diff --git a/CHANGES b/CHANGES index d2d9304e73..921fbec76b 100644 --- a/CHANGES +++ b/CHANGES @@ -72,7 +72,11 @@ CHANGES - Query won't fail with weakref error when a non-mapper/class instrumented descriptor is passed, raises "Invalid column expession". - + + - Query.group_by() properly takes into account aliasing applied + to the FROM clause, such as with select_from(), using + with_polymorphic(), or using from_self(). + - sql - Fixed missing _label attribute on Function object, others when used in a select() with use_labels (such as when used diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index 5bef348a1f..a8be43f5a1 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -697,6 +697,8 @@ class Query(object): criterion = list(chain(*[_orm_columns(c) for c in criterion])) + criterion = [self._adapt_clause(expression._literal_as_text(o), True, True) for o in criterion] + if self._group_by is False: self._group_by = criterion else: diff --git a/test/orm/query.py b/test/orm/query.py index d54cf4f713..d5ed96d4b5 100644 --- a/test/orm/query.py +++ b/test/orm/query.py @@ -773,6 +773,20 @@ class FromSelfTest(QueryTest, AssertsCompiledSQL): ] == create_session().query(User).filter(User.id.in_([8,9]))._from_self().\ join('addresses').add_entity(Address).order_by(User.id, Address.id).all() + def test_group_by(self): + eq_( + create_session().query(Address.user_id, func.count(Address.id).label('count')).\ + group_by(Address.user_id).order_by(Address.user_id).all(), + [(7, 1), (8, 3), (9, 1)] + ) + + eq_( + create_session().query(Address.user_id, Address.id).\ + from_self(Address.user_id, func.count(Address.id)).\ + group_by(Address.user_id).order_by(Address.user_id).all(), + [(7, 1), (8, 3), (9, 1)] + ) + def test_no_eagerload(self): """test that eagerloads are pushed outwards and not rendered in subqueries."""