Opened 3 years ago

Closed 3 years ago

Last modified 3 years 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 by Iuri de Silvio, 3 years ago

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

comment:2 by Iuri de Silvio, 3 years ago

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 by Mariusz Felisiak, 3 years ago

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 by Iuri de Silvio, 3 years ago

Patch needs improvement: unset

comment:5 by Mariusz Felisiak, 3 years ago

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 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In 9760e26:

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

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

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