]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Query.group_by() properly takes into account aliasing applied
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 8 Mar 2009 19:06:12 +0000 (19:06 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 8 Mar 2009 19:06:12 +0000 (19:06 +0000)
to the FROM clause, such as with select_from(), using
with_polymorphic(), or using from_self().

CHANGES
lib/sqlalchemy/orm/query.py
test/orm/query.py

diff --git a/CHANGES b/CHANGES
index d2d9304e731e95f9b49abee0afc0f292a8c9a18d..921fbec76b882b35ce3c5f6fb308ee036f7d21f9 100644 (file)
--- 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
index 5bef348a1ff5b92898cb768e2bc17af3a691bd2d..a8be43f5a1ccc57f85fff132b936260bc2f2c329 100644 (file)
@@ -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:
index d54cf4f713c0e36dd825207e70986927ae01f27d..d5ed96d4b520dc99c9a0435a690291dfb37ac7eb 100644 (file)
@@ -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."""