]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
order_by and group_by being output in wrong order. Added tests for same.
authorRobert Leftwich <rtl@pobox.com>
Sun, 22 Jan 2006 12:08:10 +0000 (12:08 +0000)
committerRobert Leftwich <rtl@pobox.com>
Sun, 22 Jan 2006 12:08:10 +0000 (12:08 +0000)
lib/sqlalchemy/sql.py
test/select.py

index 49040ec93fb81331f64b61ab874c4496bcea4f26..c37a8d620f97301ca5bb83967f9f2402e395265e 100644 (file)
@@ -1119,15 +1119,15 @@ class CompoundSelect(SelectBaseMixin, FromClause):
         self.use_labels = kwargs.pop('use_labels', False)
         self.oid_column = selects[0].oid_column
         for s in self.selects:
-            s.order_by(None)
             s.group_by(None)
+            s.order_by(None)
         self.clauses = []
-        order_by = kwargs.get('order_by', None)
-        if order_by:
-            self.order_by(*order_by)
         group_by = kwargs.get('group_by', None)
         if group_by:
             self.group_by(*group_by)
+        order_by = kwargs.get('order_by', None)
+        if order_by:
+            self.order_by(*order_by)
     def hash_key(self):
         return "CompoundSelect(%s)" % string.join(
             [util.hash_key(s) for s in self.selects] + 
@@ -1196,10 +1196,10 @@ class Select(SelectBaseMixin, FromClause):
         for f in from_obj:
             self.append_from(f)
 
-        if order_by:
-            self.order_by(*order_by)
         if group_by:
             self.group_by(*group_by)
+        if order_by:
+            self.order_by(*order_by)
             
     class CorrelatedVisitor(ClauseVisitor):
         """visits a clause, locates any Select clauses, and tells them that they should
index 6f4557f5eebc9309ced833d825feb86731c65773..625a1ec7cf459405c179514524482c3411f37304 100644 (file)
@@ -176,6 +176,16 @@ sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") A
             table2.select(order_by = [table2.c.id, asc(table2.c.name)]),
             "SELECT myothertable.otherid, myothertable.othername FROM myothertable ORDER BY myothertable.otherid, myothertable.othername ASC"
         )
+    def testgroupby(self):
+        self.runtest(
+            select([table2.c.name, func.count(table2.c.id)], group_by = [table2.c.name]),
+            "SELECT myothertable.othername, count(myothertable.otherid) FROM myothertable GROUP BY myothertable.othername"
+        )
+    def testgroupby_and_orderby(self):
+        self.runtest(
+            select([table2.c.name, func.count(table2.c.id)], group_by = [table2.c.name], order_by = [table2.c.name]),
+            "SELECT myothertable.othername, count(myothertable.otherid) FROM myothertable GROUP BY myothertable.othername ORDER BY myothertable.othername"
+        )
     def testalias(self):
         # test the alias for a table.  column names stay the same, table name "changes" to "foo".
         self.runtest(