- query doesn't throw an error if you use distinct() and an order_by()
containing UnaryExpressions (or other) together [ticket:848]
-
+
+ - order_by() expressions from joined tables are properly added to columns
+ clause when using distinct() [ticket:786]
+
- fixed error where Query.add_column() would not accept a class-bound
attribute as an argument; Query also raises an error if an invalid
argument was sent to add_column() (at instances() time) [ticket:858]
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()
+
+ def test_joined(self):
+ """test that orderbys from a joined table get placed into the columns clause when DISTINCT is used"""
+
+ sess = create_session()
+ q = sess.query(User).join('addresses').distinct().order_by(desc(Address.email_address))
+
+ assert [User(id=7), User(id=9), User(id=8)] == q.all()
+
+ sess.clear()
+
+ # test that it works on embedded eagerload/LIMIT subquery
+ q = sess.query(User).join('addresses').distinct().options(eagerload('addresses')).order_by(desc(Address.email_address)).limit(2)
+
+ def go():
+ assert [
+ User(id=7, addresses=[
+ Address(id=1)
+ ]),
+ User(id=9, addresses=[
+ Address(id=5)
+ ]),
+ ] == q.all()
+ self.assert_sql_count(testbase.db, go, 1)
class TextTest(QueryTest):