Fix query without order by handling

This commit is contained in:
Konsta Vesterinen
2014-10-24 14:44:30 +03:00
parent 66538cddd0
commit 6c3ad4e51e
3 changed files with 14 additions and 12 deletions

View File

@@ -8,6 +8,7 @@ Here you can see the full list of changes between each SQLAlchemy-Utils release.
^^^^^^^^^^^^^^^^^^^
- Made assert_* functions automatically rollback session
- Changed make_order_by_deterministic attach order by primary key for queries without order by
- Fixed alias handling in has_unique_index
- Fixed alias handling in has_index
- Fixed alias handling in make_order_by_deterministic

View File

@@ -169,19 +169,20 @@ def make_order_by_deterministic(query):
.. versionadded: 0.27.1
"""
if not query._order_by:
return query
order_by_func = sa.asc
order_by = query._order_by[0]
if isinstance(order_by, sa.sql.expression.UnaryExpression):
if order_by.modifier == sa.sql.operators.desc_op:
order_by_func = sa.desc
else:
order_by_func = sa.asc
column = order_by.get_children()[0]
if not query._order_by:
column = None
else:
column = order_by
order_by_func = sa.asc
order_by = query._order_by[0]
if isinstance(order_by, sa.sql.expression.UnaryExpression):
if order_by.modifier == sa.sql.operators.desc_op:
order_by_func = sa.desc
else:
order_by_func = sa.asc
column = order_by.get_children()[0]
else:
column = order_by
# Queries that are ordered by an already
if isinstance(column, sa.Column):

View File

@@ -82,7 +82,7 @@ class TestMakeOrderByDeterministic(TestCase):
def test_query_without_order_by(self):
query = self.session.query(self.User)
query = make_order_by_deterministic(query)
assert 'ORDER BY' not in str(query)
assert 'ORDER BY "user".id' in str(query)
def test_alias(self):
alias = sa.orm.aliased(self.User.__table__)