{{{
>>> 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.
* type: Uncategorized => Bug
* component: Uncategorized => Database layer (models, ORM)
--
Ticket URL: <https://code.djangoproject.com/ticket/34615#comment:1>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/34615#comment:2>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/34615#comment:3>
* status: assigned => closed
* resolution: => wontfix
Comment:
We added `order_by("pk")` only when it's not explicitly ordered, mainly,
to get the consistent and stable behavior on all backends. It's been that
way basically forever and, IMO, we shouldn't change it. You can start a
discussion on DevelopersMailingList if you don't agree.
--
Ticket URL: <https://code.djangoproject.com/ticket/34615#comment:4>
Comment (by Iuri de Silvio):
Thanks! I disagree a bit because if I'm explicitly disabling ordering with
an empty `order_by`, it is not the same as not defining ordering.
I'll follow up on mailing list.
--
Ticket URL: <https://code.djangoproject.com/ticket/34615#comment:5>
Comment (by Mariusz Felisiak):
See related ticket #23805.
--
Ticket URL: <https://code.djangoproject.com/ticket/34615#comment:6>
Comment (by David Sanders):
> Thanks! I disagree a bit because if I'm explicitly disabling ordering
with an empty order_by, it is not the same as not defining ordering.
I get your original point, but I agree with Mariusz and the original
authors adding this behaviour.
To have a first/last you must have deterministic ordering and not whatever
the database feels like it wants to do otherwise this will start to
introduce subtle soul-destroying bugs. I've numerous tests fail
intermittently (with the same db) because we forgot to add explicit
ordering.
--
Ticket URL: <https://code.djangoproject.com/ticket/34615#comment:7>