diff options
-rw-r--r-- | CHANGES | 3 | ||||
-rw-r--r-- | lib/sqlalchemy/orm/query.py | 9 | ||||
-rw-r--r-- | test/orm/query.py | 6 |
3 files changed, 15 insertions, 3 deletions
@@ -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 diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index c8a1f9cf8..753e735d1 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -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 diff --git a/test/orm/query.py b/test/orm/query.py index d879f7192..a8790d2cf 100644 --- a/test/orm/query.py +++ b/test/orm/query.py @@ -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() |