Skip to content

Commit f7b7995

Browse files
committed
Fix AbstractQuerySet.order_by to consider existing conditions
Scylla does not tolerate queries with repeated column, like one below: `ORDER BY "attempt_id" ASC, "attempt_id" DESC`
1 parent dc299d0 commit f7b7995

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

cassandra/cqlengine/query.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -819,12 +819,19 @@ class Comment(Model):
819819
clone._order = []
820820
return clone
821821

822-
conditions = []
823-
for colname in colnames:
824-
conditions.append('"{0}" {1}'.format(*self._get_ordering_condition(colname)))
825-
826822
clone = copy.deepcopy(self)
827-
clone._order.extend(conditions)
823+
824+
for col_name in colnames:
825+
col_name, order = self._get_ordering_condition(col_name)
826+
col_name = '"{0}"'.format(col_name)
827+
# Check if there is existing condition that targets same column and replace it if it's order is different
828+
for cond_id, existing_condition in enumerate(clone._order):
829+
if col_name in existing_condition:
830+
if not existing_condition.endswith(order):
831+
clone._order[cond_id] = '{0} {1}'.format(col_name, order)
832+
break
833+
else:
834+
clone._order.append('{0} {1}'.format(col_name, order))
828835
return clone
829836

830837
def count(self):

0 commit comments

Comments
 (0)