mirror of
https://github.com/django/django.git
synced 2025-10-27 23:56:08 +00:00
Fixed #36407 -- Ensured default value is cast in Case expressions used in ORDER BY clause.
Thanks to deceze for the report. Thanks to Sarah Boyce for the test. Thanks to Simon Charette for the investigation and review.
This commit is contained in:
@@ -3,15 +3,18 @@ from operator import attrgetter
|
||||
|
||||
from django.core.exceptions import FieldError
|
||||
from django.db.models import (
|
||||
Case,
|
||||
CharField,
|
||||
Count,
|
||||
DateTimeField,
|
||||
F,
|
||||
IntegerField,
|
||||
Max,
|
||||
OrderBy,
|
||||
OuterRef,
|
||||
Subquery,
|
||||
Value,
|
||||
When,
|
||||
)
|
||||
from django.db.models.functions import Length, Upper
|
||||
from django.test import TestCase
|
||||
@@ -526,6 +529,17 @@ class OrderingTests(TestCase):
|
||||
qs = Article.objects.order_by(Value("1", output_field=CharField()), "-headline")
|
||||
self.assertSequenceEqual(qs, [self.a4, self.a3, self.a2, self.a1])
|
||||
|
||||
def test_order_by_case_when_constant_value(self):
|
||||
qs = Article.objects.order_by(
|
||||
Case(
|
||||
When(pk__in=[], then=Value(1)),
|
||||
default=Value(0),
|
||||
output_field=IntegerField(),
|
||||
).desc(),
|
||||
"pk",
|
||||
)
|
||||
self.assertSequenceEqual(qs, [self.a1, self.a2, self.a3, self.a4])
|
||||
|
||||
def test_related_ordering_duplicate_table_reference(self):
|
||||
"""
|
||||
An ordering referencing a model with an ordering referencing a model
|
||||
|
||||
Reference in New Issue
Block a user