Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions django/db/backends/oracle/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
13 changes: 9 additions & 4 deletions django/db/models/sql/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -640,6 +636,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
Expand Down
11 changes: 3 additions & 8 deletions tests/queries/test_qs_combinators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
Loading