mirror of
https://github.com/django/django.git
synced 2024-12-22 17:16:24 +00:00
Fixed #28900 -- Propagated all selected fields to combinator queries.
Previously, only the selected column aliases would be propagated and annotations were ignored.
This commit is contained in:
parent
65ad4ade74
commit
6d220963fa
@ -591,20 +591,15 @@ class SQLCompiler:
|
||||
# generate valid SQL.
|
||||
compiler.elide_empty = False
|
||||
parts = ()
|
||||
selected = self.query.selected
|
||||
for compiler in compilers:
|
||||
try:
|
||||
# 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 self.query.values_select:
|
||||
if selected is not None and compiler.query.selected is None:
|
||||
compiler.query = compiler.query.clone()
|
||||
compiler.query.set_values(
|
||||
(
|
||||
*self.query.extra_select,
|
||||
*self.query.values_select,
|
||||
*self.query.annotation_select,
|
||||
)
|
||||
)
|
||||
compiler.query.set_values(selected)
|
||||
part_sql, part_args = compiler.as_sql(with_col_aliases=True)
|
||||
if compiler.query.combinator:
|
||||
# Wrap in a subquery if wrapping in parentheses isn't
|
||||
|
@ -282,11 +282,7 @@ class QuerySetSetOperationTests(TestCase):
|
||||
)
|
||||
.values_list("num", "count")
|
||||
)
|
||||
qs2 = (
|
||||
Number.objects.filter(num=2)
|
||||
.extra(select={"count": 1})
|
||||
.values_list("num", "count")
|
||||
)
|
||||
qs2 = Number.objects.filter(num=2).extra(select={"count": 1})
|
||||
self.assertCountEqual(qs1.union(qs2), [(1, 0), (2, 1)])
|
||||
|
||||
def test_union_with_values_list_on_annotated_and_unannotated(self):
|
||||
|
@ -1375,6 +1375,16 @@ class Queries1Tests(TestCase):
|
||||
self.assertCountEqual(items_after, [self.i2, self.i3, self.i4])
|
||||
self.assertCountEqual(items_before, items_after)
|
||||
|
||||
def test_union_values_subquery(self):
|
||||
items = Item.objects.filter(creator=OuterRef("pk"))
|
||||
item_authors = Author.objects.annotate(is_creator=Exists(items)).order_by()
|
||||
reports = Report.objects.filter(creator=OuterRef("pk"))
|
||||
report_authors = Author.objects.annotate(is_creator=Exists(reports)).order_by()
|
||||
all_authors = item_authors.union(report_authors).order_by("is_creator")
|
||||
self.assertEqual(
|
||||
list(all_authors.values_list("is_creator", flat=True)), [False, True]
|
||||
)
|
||||
|
||||
|
||||
class Queries2Tests(TestCase):
|
||||
@classmethod
|
||||
|
Loading…
Reference in New Issue
Block a user