]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- query doesn't throw an error if you use distinct() and an order_by()
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 9 Nov 2007 16:25:28 +0000 (16:25 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 9 Nov 2007 16:25:28 +0000 (16:25 +0000)
containing UnaryExpressions (or other) together [ticket:848]

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

diff --git a/CHANGES b/CHANGES
index e29a5c561212e81cab4ac48e6e2aa34c1cb6d438..b6a9024e96bdf87a4a41d7aa4394c2143648c803 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -71,6 +71,9 @@ CHANGES
     only those paths, i.e. and not 'x.y.x'; eagerload('children.children')
     applies only to exactly two-levels deep, etc. [ticket:777]
 
+  - query doesn't throw an error if you use distinct() and an order_by()
+    containing UnaryExpressions (or other) together [ticket:848]
+    
   - The session API has been solidified:
 
     - It's an error to session.save() an object which is already
index c8a1f9cf86c36be036e66123a800813a1c750089..753e735d1d965c3f1ec3630f735f58e0f1f4e8de 100644 (file)
@@ -896,10 +896,13 @@ class Query(object):
                 
             # for a DISTINCT query, you need the columns explicitly specified in order
             # to use it in "order_by".  ensure they are in the column criterion (particularly oid).
-            # TODO: this should be done at the SQL level not the mapper level
-            # TODO: need test coverage for this 
             if self._distinct and order_by:
-                [statement.append_column(c) for c in util.to_list(order_by)]
+                order_by = [expression._literal_as_text(o) for o in util.to_list(order_by) or []]
+                cf = sql_util.ColumnFinder()
+                for o in order_by:
+                    cf.traverse(o)
+
+                [statement.append_column(c) for c in cf]
 
         context.statement = statement
                 
index d879f71928dc9791955f1931cd0736febcf7b119..a8790d2cfa2af65966a5630949021cd8e560f1e1 100644 (file)
@@ -356,6 +356,12 @@ class CountTest(QueryTest):
 
         assert 2 == create_session().query(User).filter(users.c.name.endswith('ed')).count()
 
+class DistinctTest(QueryTest):
+    def test_basic(self):
+        assert [User(id=7), User(id=8), User(id=9),User(id=10)] == create_session().query(User).distinct().all()
+        assert [User(id=7), User(id=9), User(id=8),User(id=10)] == create_session().query(User).distinct().order_by(desc(User.name)).all()
+        
+    
 class TextTest(QueryTest):
     def test_fulltext(self):
         assert [User(id=7), User(id=8), User(id=9),User(id=10)] == create_session().query(User).from_statement("select * from users").all()