#34123: Regression: Ordering on combined queries with select_related
-------------------------------------+-------------------------------------
Reporter: Shai | Owner: nobody
Berger |
Type: Bug | Status: new
Component: Database | Version: dev
layer (models, ORM) |
Severity: Release | Keywords: select_related
blocker |
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Ordering on combined queries with {{{select_related()}}} seems to have
been slightly broken in [c58a8acd413ccc].
Consider these tests:
{{{#!python
# Model Author has `ordering = ["name"]` in its Meta
def test_union_with_select_related_and_order(self):
base_qs = Author.objects.select_related('extra').order_by()
qs1 = base_qs.filter(name="a1")
qs2 = base_qs.filter(name="a2")
self.assertFalse(
bool(
qs1.union(qs2).order_by("pk")
)
)
# first() also calls order_by("pk")
def test_union_with_select_related_and_first(self):
base_qs = Author.objects.select_related('extra')
qs1 = base_qs.filter(name="a1")
qs2 = base_qs.filter(name="a2")
self.assertFalse(
bool(
qs1.union(qs2).first()
)
)
}}}
On Postrgres, both of these pass before the named commit, and error after
it with
{{{
django.db.utils.ProgrammingError: ORDER BY "id" is ambiguous
LINE 1: ...id") WHERE "queries_author"."name" = 'a2') ORDER BY "id" ASC
}}}
On Sqlite, the second test (where ordering from the model is in force)
breaks even before this, with
{{{
django.db.utils.DatabaseError: ORDER BY not allowed in subqueries of
compound statements.
}}}
but on current main, the first breaks too -- this time, with
{{{
django.db.utils.OperationalError: 1st ORDER BY term does not match any
column in the result set
}}}
Note that on union queries without {{{select_related()}}}, ordering on
{{{pk}}} works -- many existing tests use it, as well as another test I
added (in the attached diff) just to be sure.
--
Ticket URL: <https://code.djangoproject.com/ticket/34123>
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/0107018418abf9b1-bdf6d825-115e-4b8f-9708-ff1523ba1f76-000000%40eu-central-1.amazonses.com.