mirror of
https://github.com/django/django.git
synced 2025-10-27 15:46:10 +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:
@@ -1711,14 +1711,24 @@ class Case(SQLiteNumericMixin, Expression):
|
||||
except EmptyResultSet:
|
||||
continue
|
||||
except FullResultSet:
|
||||
default_sql, default_params = compiler.compile(case.result)
|
||||
default = case.result
|
||||
break
|
||||
case_parts.append(case_sql)
|
||||
sql_params.extend(case_params)
|
||||
else:
|
||||
default_sql, default_params = compiler.compile(self.default)
|
||||
if not case_parts:
|
||||
return default_sql, default_params
|
||||
default = self.default
|
||||
if case_parts:
|
||||
default_sql, default_params = compiler.compile(default)
|
||||
else:
|
||||
if (
|
||||
isinstance(default, Value)
|
||||
and (output_field := default._output_field_or_none) is not None
|
||||
):
|
||||
from django.db.models.functions import Cast
|
||||
|
||||
default = Cast(default, output_field)
|
||||
return compiler.compile(default)
|
||||
|
||||
case_joiner = case_joiner or self.case_joiner
|
||||
template_params["cases"] = case_joiner.join(case_parts)
|
||||
template_params["default"] = default_sql
|
||||
|
||||
Reference in New Issue
Block a user