Skip to content

Commit 802baf5

Browse files
Colleen Dunlapsarahboyce
authored andcommitted
Fixed #36388 -- Made QuerySet.union() return self when called with no arguments.
Regression in 9cb8baa. Thank you to Antoine Humeau for the report and Simon Charette for the review.
1 parent 1ba5fe1 commit 802baf5

File tree

4 files changed

+13
-0
lines changed

4 files changed

+13
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ answer newbie questions, and generally made Django that much better:
239239
Claude Paroz <[email protected]>
240240
Clifford Gama <[email protected]>
241241
Clint Ecker
242+
Colleen Dunlap <https://medium.com/@colleen85052>
242243
243244
Colin Wood <[email protected]>
244245
Collin Anderson <[email protected]>

django/db/models/query.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,8 @@ def union(self, *other_qs, all=False):
15501550
if len(qs) == 1:
15511551
return qs[0]
15521552
return qs[0]._combinator_query("union", *qs[1:], all=all)
1553+
elif not other_qs:
1554+
return self
15531555
return self._combinator_query("union", *other_qs, all=all)
15541556

15551557
def intersection(self, *other_qs):

docs/releases/5.2.2.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ Bugfixes
1515
* Fixed a bug in Django 5.2 where subqueries using ``"pk"`` to reference models
1616
with a ``CompositePrimaryKey`` failed to raise ``ValueError`` when too many
1717
or too few columns were selected (:ticket:`36392`).
18+
19+
* Fixed a regression in Django 5.2 that caused a crash when no arguments were
20+
passed into ``QuerySet.union()`` (:ticket:`36388`).

tests/queries/test_qs_combinators.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ def test_union_none_slice(self):
8888
qs3 = qs1.union(qs2)
8989
self.assertNumbersEqual(qs3[:1], [0])
9090

91+
def test_union_empty_slice(self):
92+
qs = Number.objects.union()
93+
self.assertNumbersEqual(qs[:1], [0])
94+
qs = Number.objects.union(all=True)
95+
self.assertNumbersEqual(qs[:1], [0])
96+
self.assertNumbersEqual(qs.order_by("num")[0:], list(range(0, 10)))
97+
9198
def test_union_all_none_slice(self):
9299
qs = Number.objects.filter(id__in=[])
93100
with self.assertNumQueries(0):

0 commit comments

Comments
 (0)