From 9760e262f85ae57df39abe2799eff48a82b14474 Mon Sep 17 00:00:00 2001 From: Iuri de Silvio Date: Thu, 8 Apr 2021 16:31:45 -0300 Subject: [PATCH] Fixed #32627 -- Fixed QuerySet.values()/values_list() crash on combined querysets ordered by unannotated columns. --- django/db/models/sql/compiler.py | 14 ++++++++++---- docs/releases/3.2.1.txt | 5 +++++ tests/queries/test_qs_combinators.py | 20 +++++++++++++++++++- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index 11ad4fde90..e311b7998a 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -347,10 +347,16 @@ class SQLCompiler: continue if not self.query.extra or col not in self.query.extra: - # 'col' is of the form 'field' or 'field1__field2' or - # '-field1__field2__field', etc. - order_by.extend(self.find_ordering_name( - field, self.query.get_meta(), default_order=asc)) + if self.query.combinator and self.select: + # Don't use the first model's field because other + # combinated queries might define it differently. + order_by.append((OrderBy(F(col), descending=descending), False)) + else: + # 'col' is of the form 'field' or 'field1__field2' or + # '-field1__field2__field', etc. + order_by.extend(self.find_ordering_name( + field, self.query.get_meta(), default_order=asc, + )) else: if col not in self.query.extra_select: order_by.append(( diff --git a/docs/releases/3.2.1.txt b/docs/releases/3.2.1.txt index e8361e7370..2a7dda13ab 100644 --- a/docs/releases/3.2.1.txt +++ b/docs/releases/3.2.1.txt @@ -14,3 +14,8 @@ Bugfixes * Fixed a bug in Django 3.2 where subclasses of ``BigAutoField`` and ``SmallAutoField`` were not allowed for the :setting:`DEFAULT_AUTO_FIELD` setting (:ticket:`32620`). + +* Fixed a regression in Django 3.2 that caused a crash of + ``QuerySet.values()/values_list()`` after ``QuerySet.union()``, + ``intersection()``, and ``difference()`` when it was ordered by an + unannotated field (:ticket:`32627`). diff --git a/tests/queries/test_qs_combinators.py b/tests/queries/test_qs_combinators.py index 3b8204645d..351a401902 100644 --- a/tests/queries/test_qs_combinators.py +++ b/tests/queries/test_qs_combinators.py @@ -5,7 +5,7 @@ from django.db.models import Exists, F, IntegerField, OuterRef, Value from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature from django.test.utils import CaptureQueriesContext -from .models import Number, ReservedName +from .models import Celebrity, Number, ReservedName @skipUnlessDBFeature('supports_select_union') @@ -234,6 +234,24 @@ class QuerySetSetOperationTests(TestCase): operator.itemgetter('num'), ) + def test_union_multiple_models_with_values_list_and_order(self): + reserved_name = ReservedName.objects.create(name='rn1', order=0) + qs1 = Celebrity.objects.all() + qs2 = ReservedName.objects.all() + self.assertSequenceEqual( + qs1.union(qs2).order_by('name').values_list('pk', flat=True), + [reserved_name.pk], + ) + + def test_union_multiple_models_with_values_list_and_order_by_extra_select(self): + reserved_name = ReservedName.objects.create(name='rn1', order=0) + qs1 = Celebrity.objects.extra(select={'extra_name': 'name'}) + qs2 = ReservedName.objects.extra(select={'extra_name': 'name'}) + self.assertSequenceEqual( + qs1.union(qs2).order_by('extra_name').values_list('pk', flat=True), + [reserved_name.pk], + ) + def test_count_union(self): qs1 = Number.objects.filter(num__lte=1).values('num') qs2 = Number.objects.filter(num__gte=2, num__lte=3).values('num')