summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2018-04-13 12:15:52 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2018-04-13 12:54:32 +0200
commitf89b11b879b83aa505dc8231da5f06ca4b1b062e (patch)
tree7513e4412c5cbfc8da890248d4d7bf5fecab75a4
parent46496a542c2ff9f273e090073e9c8071acb1a4a4 (diff)
[1.11.x] Fixed #29286 -- Fixed column mismatch crash with QuerySet.values() or values_list() after combining an annotated and unannotated queryset with union(), difference(), or intersection().
Regression in a0c03c62a8ac586e5be5b21393c925afa581efaf. Thanks Tim Graham and Carlton Gibson for reviews. Backport of 0b66c3b442875627fa6daef4ac1e90900d74290b from master.
-rw-r--r--django/db/models/sql/compiler.py5
-rw-r--r--docs/releases/1.11.13.txt5
-rw-r--r--tests/queries/test_qs_combinators.py10
3 files changed, 16 insertions, 4 deletions
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
index 033ea984bd..edfe22e366 100644
--- a/django/db/models/sql/compiler.py
+++ b/django/db/models/sql/compiler.py
@@ -393,9 +393,8 @@ class SQLCompiler(object):
# If the columns list is limited, then all combined queries
# must have the same columns list. Set the selects defined on
# the query on all combined queries, if not already set.
- if (not compiler.query.values_select and not compiler.query.annotations and
- self.query.values_select):
- compiler.query.set_values(self.query.values_select)
+ if not compiler.query.values_select and self.query.values_select:
+ compiler.query.set_values(tuple(self.query.values_select) + tuple(self.query.annotation_select))
parts += (compiler.as_sql(),)
except EmptyResultSet:
# Omit the empty queryset with UNION and with DIFFERENCE if the
diff --git a/docs/releases/1.11.13.txt b/docs/releases/1.11.13.txt
index f72ccd8fd2..4c55454bcd 100644
--- a/docs/releases/1.11.13.txt
+++ b/docs/releases/1.11.13.txt
@@ -15,3 +15,8 @@ Bugfixes
* Fixed crashes in ``django.contrib.admindocs`` when a view is a callable
object, such as ``django.contrib.syndication.views.Feed`` (:ticket:`29296`).
+
+* Fixed a regression in Django 1.11.12 where ``QuerySet.values()`` or
+ ``values_list()`` after combining an annotated and unannotated queryset with
+ ``union()``, ``difference()``, or ``intersection()`` crashed due to mismatching
+ columns (:ticket:`29286`).
diff --git a/tests/queries/test_qs_combinators.py b/tests/queries/test_qs_combinators.py
index 9083520c24..94af81a49b 100644
--- a/tests/queries/test_qs_combinators.py
+++ b/tests/queries/test_qs_combinators.py
@@ -1,6 +1,6 @@
from __future__ import unicode_literals
-from django.db.models import F, IntegerField, Value
+from django.db.models import Exists, F, IntegerField, OuterRef, Value
from django.db.utils import DatabaseError
from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
from django.utils.six.moves import range
@@ -139,6 +139,14 @@ class QuerySetSetOperationTests(TestCase):
).values_list('num', 'count')
self.assertCountEqual(qs1.union(qs2), [(1, 0), (2, 1)])
+ def test_union_with_values_list_on_annotated_and_unannotated(self):
+ ReservedName.objects.create(name='rn1', order=1)
+ qs1 = Number.objects.annotate(
+ has_reserved_name=Exists(ReservedName.objects.filter(order=OuterRef('num')))
+ ).filter(has_reserved_name=True)
+ qs2 = Number.objects.filter(num=9)
+ self.assertCountEqual(qs1.union(qs2).values_list('num', flat=True), [1, 9])
+
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')