#20600: Subqueries should retain ORDER BY when using DISTINCT ON
-------------------------------------+-------------------------------------
Reporter: brianglass | Owner: anonymous
Type: Bug | Status: assigned
Component: Database layer | Version: 1.5
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 1
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Comment (by chekunkov):
I've got this issue as well. Posted workaround didn't work for me (even
with .values()). The only solution I've found is to evaluate query for ids
and then use result in "in" statement:
{{{
top_chapters_ids = Chapter.objects.filter(
topics__in=['css', 'javascript']
).distinct(
'book_id'
).order_by(
'book_id',
'-weight',
'id'
).values_list('id', flat=True)
# evaluate query
top_chapters_ids = list(top_chapters_ids)
chapters = Chapter.objects.filter(
id__in=top_chapters_ids
).order_by('weight')
}}}
It is working but resource-consuming solution. And possibly with large
number of ids it wouldn't work for SQLite databases, because of their
limitations.
--
Ticket URL: <https://code.djangoproject.com/ticket/20600#comment:7>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/068.c3b75cc87faf6cc4ef7b1582474eea4b%40djangoproject.com.
For more options, visit https://groups.google.com/groups/opt_out.