Opened 6 months ago

Closed 6 months ago

Last modified 6 months ago

#32627 closed Bug (fixed)

QuerySet.values()/values_list() crash on combined querysets ordered by unannotated columns.

Reported by: Iuri de Silvio Owned by: Iuri de Silvio
Component: Database layer (models, ORM) Version: 3.2
Severity: Release blocker Keywords:
Cc: David Wobrock Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Iuri de Silvio)

Django 3.2 fails with this query:

qs1 = Celebrity.objects.all()
qs2 = ReservedName.objects.all()
qs1.union(qs2).values_list('name').first()

It worked until Django 3.1.8. This commit[1] to be exactly. https://github.com/django/django/commit/464a4c0c59277056b5d3c1132ac1b4c6085aee08

This is the broken generated query. In the second query, it fetches from the first table.

SQL
SELECT
    "queries_celebrity"."name",
    "queries_celebrity"."id" AS "__orderbycol2"
FROM
    "queries_celebrity"
UNION
SELECT
    "queries_reservedname"."name",
    "queries_celebrity"."id" AS "__orderbycol2"  -- HERE IS THE PROBLEM
FROM
    "queries_reservedname"
ORDER BY
    (2) ASC
LIMIT
    1

Before, it was:

SQL
SELECT
    "queries_celebrity"."name",
    "queries_celebrity"."id"
FROM
    "queries_celebrity"
UNION
SELECT
    "queries_reservedname"."name",
    "queries_reservedname"."id"
FROM
    "queries_reservedname"
ORDER BY
    (2) ASC
LIMIT
    1

Change History (7)

comment:1 Changed 6 months ago by Iuri de Silvio

Description: modified (diff)
Owner: changed from nobody to Iuri de Silvio
Status: newassigned

comment:2 Changed 6 months ago by Iuri de Silvio

I did a hacky patch just to make it work with minimal changes. https://github.com/django/django/pull/14241/commits/9d49fbfee7053c0a17108fe7a908b8956a6181dc

It was easier for me to understand the fix, but I'll cleanup the code.

comment:3 Changed 6 months ago by Mariusz Felisiak

Cc: David Wobrock added
Has patch: set
Patch needs improvement: set
Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

Thanks for this report.

PR

Regression in 464a4c0c59277056b5d3c1132ac1b4c6085aee08.
Reproduced at 1c004939d5dd781fc16a8b4fb9696661cf545929.

comment:4 Changed 6 months ago by Iuri de Silvio

Patch needs improvement: unset

comment:5 Changed 6 months ago by Mariusz Felisiak

Summary: Multi-model union fails with .first()QuerySet.values()/values_list() crash on combined querysets ordered by unannotated columns.
Triage Stage: AcceptedReady for checkin

comment:6 Changed 6 months ago by Mariusz Felisiak <felisiak.mariusz@…>

Resolution: fixed
Status: assignedclosed

In 9760e26:

Fixed #32627 -- Fixed QuerySet.values()/values_list() crash on combined querysets ordered by unannotated columns.

comment:7 Changed 6 months ago by Mariusz Felisiak <felisiak.mariusz@…>

In b2458455:

[3.2.x] Fixed #32627 -- Fixed QuerySet.values()/values_list() crash on combined querysets ordered by unannotated columns.

Backport of 9760e262f85ae57df39abe2799eff48a82b14474 from main

Note: See TracTickets for help on using tickets.
Back to Top