From 9c655e98006dec4f4fd47a84c254f36404d631e4 Mon Sep 17 00:00:00 2001 From: siddus Date: Mon, 13 Apr 2026 11:32:43 -0400 Subject: [PATCH 1/2] Fixed #36938 -- Removed unnecessary ordering from compound queries. --- django/db/backends/oracle/features.py | 3 --- django/db/models/sql/compiler.py | 9 +++++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/django/db/backends/oracle/features.py b/django/db/backends/oracle/features.py index 55c64a818d18..81bb28ea2a85 100644 --- a/django/db/backends/oracle/features.py +++ b/django/db/backends/oracle/features.py @@ -86,9 +86,6 @@ class DatabaseFeatures(BaseDatabaseFeatures): "annotations.tests.NonAggregateAnnotationTestCase.test_custom_functions", "annotations.tests.NonAggregateAnnotationTestCase." "test_custom_functions_can_ref_other_functions", - # A bug in Django with respect to unioning ordered querysets (#36938). - "queries.test_qs_combinators.QuerySetSetOperationTests." - "test_count_union_with_select_related_in_values", } insert_test_table_with_defaults = ( "INSERT INTO {} VALUES (DEFAULT, DEFAULT, DEFAULT)" diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index bcf28f9ae16d..6442ed7ecbd3 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -640,6 +640,15 @@ def _get_combinator_part_sql(self, compiler): if selected is not None and compiler.query.selected is None: compiler.query = compiler.query.clone() compiler.query.set_values(selected) + if ( + ( + features.requires_compound_order_by_subquery + and not features.ignores_unnecessary_order_by_in_subqueries + ) + or not features.supports_parentheses_in_compound + ) and compiler.get_order_by(): + compiler.query = compiler.query.clone() + compiler.query.clear_ordering(force=False) 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 From 2314cdf1ff860058a6579bb9f9bac1253fc9ab43 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Mon, 11 May 2026 12:07:20 -0400 Subject: [PATCH 2/2] Refs #36938 -- Tolerated unnecessary ordering in compound queries on SQLite. --- django/db/models/sql/compiler.py | 4 ---- tests/queries/test_qs_combinators.py | 11 +++-------- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index 6442ed7ecbd3..764dc46cfc59 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -590,10 +590,6 @@ def get_combinator_sql(self, combinator, all): raise DatabaseError( "LIMIT/OFFSET not allowed in subqueries of compound statements." ) - if compiler.get_order_by(): - raise DatabaseError( - "ORDER BY not allowed in subqueries of compound statements." - ) parts = [] empty_compiler = None for compiler in compilers: diff --git a/tests/queries/test_qs_combinators.py b/tests/queries/test_qs_combinators.py index d1d6bfcbe3c5..044eb39cf49b 100644 --- a/tests/queries/test_qs_combinators.py +++ b/tests/queries/test_qs_combinators.py @@ -419,7 +419,6 @@ def test_union_with_select_related_and_order(self): qs2 = base_qs.filter(name="a2") self.assertSequenceEqual(qs1.union(qs2).order_by("pk"), [a1, a2]) - @skipUnlessDBFeature("supports_slicing_ordering_in_compound") def test_union_with_select_related_and_first(self): e1 = ExtraInfo.objects.create(value=7, info="e1") a1 = Author.objects.create(name="a1", num=1, extra=e1) @@ -570,7 +569,6 @@ def test_union_in_subquery_related_outerref(self): # Combined queries don't mutate. self.assertCountEqual(qs, ["a1", "a2"]) - @skipUnlessDBFeature("supports_slicing_ordering_in_compound") def test_union_in_with_ordering(self): qs1 = Number.objects.filter(num__gt=7).order_by("num") qs2 = Number.objects.filter(num__lt=2).order_by("num") @@ -607,7 +605,6 @@ def test_count_union_with_select_related(self): qs = Author.objects.select_related("extra").order_by() self.assertEqual(qs.union(qs).count(), 1) - @skipUnlessDBFeature("supports_slicing_ordering_in_compound") def test_count_union_with_select_related_in_values(self): e1 = ExtraInfo.objects.create(value=1, info="e1") a1 = Author.objects.create(name="a1", num=1, extra=e1) @@ -692,11 +689,9 @@ def test_unsupported_ordering_slicing_raises_db_error(self): msg = "LIMIT/OFFSET not allowed in subqueries of compound statements" with self.assertRaisesMessage(DatabaseError, msg): list(qs1.union(qs2[:10])) - msg = "ORDER BY not allowed in subqueries of compound statements" - with self.assertRaisesMessage(DatabaseError, msg): - list(qs1.order_by("id").union(qs2)) - with self.assertRaisesMessage(DatabaseError, msg): - list(qs1.union(qs2).order_by("id").union(qs3)) + # Unioning ordered queries is permitted. + list(qs1.order_by("id").union(qs2)) + list(qs1.union(qs2).order_by("id").union(qs3)) @skipIfDBFeature("supports_select_intersection") def test_unsupported_intersection_raises_db_error(self):