Opened 5 weeks ago
Last modified 5 weeks ago
#36644 assigned New feature
Enable using an empty order_by() to disable implicit primary key ordering in first()
| Reported by: | Lily | Owned by: | Mridul |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Jake Howard | Triage Stage: | Accepted |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
As discussed in https://github.com/django/new-features/issues/71, update the interaction between order_by and first to enable this behaviour:
# Existing
MyModel.objects.first() # first, ordered by `pk`
MyModel.objects.order_by("pk").first() # as above
MyModel.objects.order_by("name").first() # first, ordered by `name`
# New
MyModel.objects.order_by().first() # first, but in whatever order the database returns it
This will interact with default orderings (`Meta.ordering`), which will need some careful handling.
Change History (3)
comment:1 by , 5 weeks ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:2 by , 5 weeks ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
comment:3 by , 5 weeks ago
| Cc: | added |
|---|
Note:
See TracTickets
for help on using tickets.
For folks checking the forum thread, when Tom Carrick summarized the options discussed here, this was "option 2", to which I did not see specific objections afterward.
I think that looks like this:
django/db/models/query.py
tests/queries/test_qs_combinators.py
first(), a1)I think this will need a deprecation (notice the edited test). We will be enforcing some more explicitness for users who must to clear an ordering for the sake of doing a union but then need to add it back to retain the prior behavior of
first(), but I think that trade-off is worth it.