[Fixed]-Duplicate elements in Django Paginate after `order_by` call

25👍

oh, shot in the dark, but i think i might know what it is. i wasn’t able to reproduce it in sqlite but using mysql. i think mysql trying to sort on a column that has the same value has it returning the same results during slicing

the pagination splicing basically does an sql statement of

SELECT ... FROM ... WHERE (date > D OR date IS NULL) ORDER BY date ASC LIMIT X OFFSET X

But when date is null I’m not sure how mysql sorts it. So when I tried two sql queries of LIMIT 10 and LIMIT 10 OFFSET 10 it returned sets that had the same rows, while LIMIT 20 produce a unique set.

you can try to update your order_by to order_by(‘id’, ‘date’) to have it sort by a unique field first and it may fix it.

7👍

Try to use .distinct() on your query before passing it to Paginator.

Leave a comment