#34615: queryset.order_by().first() is not consistent with other queryset
behaviours
------------------------------------------+--------------------------
Reporter: Iuri de Silvio | Owner: nobody
Type: Uncategorized | Status: assigned
Component: Uncategorized | Version: 4.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------+--------------------------
For any other operation, an empty `.order_by()` remove default ordering,
but it orders by `pk` when `.first()` is called.
{{{
>>> User.objects.only("pk").order_by()[:1]
SELECT "auth_user"."id"
FROM "auth_user"
LIMIT 1
}}}
{{{
>>> User.objects.only("pk").order_by().first()
SELECT "auth_user"."id"
FROM "auth_user"
ORDER BY "auth_user"."id" ASC
LIMIT 1
}}}
I think it is an "undefined behavior" and it should build the query
without ordering.
The ~almost~ undocumented implementation for this today should be:
{{{
try:
user = User.objects.only("pk")[0]
except IndexError:
user = None
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34615>
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 view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/01070188743abec0-08fc3cec-bc73-4b05-a12d-e4a20c35ad01-000000%40eu-central-1.amazonses.com.