From 2d31b8fd60a26d64c72510613cb493131bc4b406 Mon Sep 17 00:00:00 2001 From: Robert Leftwich Date: Sun, 22 Jan 2006 12:08:10 +0000 Subject: [PATCH] order_by and group_by being output in wrong order. Added tests for same. --- lib/sqlalchemy/sql.py | 12 ++++++------ test/select.py | 10 ++++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/sqlalchemy/sql.py b/lib/sqlalchemy/sql.py index 49040ec93f..c37a8d620f 100644 --- a/lib/sqlalchemy/sql.py +++ b/lib/sqlalchemy/sql.py @@ -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 diff --git a/test/select.py b/test/select.py index 6f4557f5ee..625a1ec7cf 100644 --- a/test/select.py +++ b/test/select.py @@ -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( -- 2.47.2