From d5088f838d837fc9e3109c828f18511055f20bea Mon Sep 17 00:00:00 2001 From: Vytis Banaitis Date: Sun, 12 Feb 2017 16:43:04 +0200 Subject: [PATCH] Fixed #27828 -- Fixed a crash when subtracting Integer/DurationField from DateField on Oracle/PostgreSQL. --- django/db/models/expressions.py | 2 +- docs/releases/1.10.6.txt | 3 +++ tests/expressions/tests.py | 6 ++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py index af8891da220..07d2b904a8e 100644 --- a/django/db/models/expressions.py +++ b/django/db/models/expressions.py @@ -383,7 +383,7 @@ class CombinedExpression(Expression): return DurationExpression(self.lhs, self.connector, self.rhs).as_sql(compiler, connection) if (lhs_output and rhs_output and self.connector == self.SUB and lhs_output.get_internal_type() in {'DateField', 'DateTimeField', 'TimeField'} and - lhs_output.get_internal_type() == lhs_output.get_internal_type()): + lhs_output.get_internal_type() == rhs_output.get_internal_type()): return TemporalSubtraction(self.lhs, self.rhs).as_sql(compiler, connection) expressions = [] expression_params = [] diff --git a/docs/releases/1.10.6.txt b/docs/releases/1.10.6.txt index 483854c0e66..b549ae1b9a4 100644 --- a/docs/releases/1.10.6.txt +++ b/docs/releases/1.10.6.txt @@ -14,3 +14,6 @@ Bugfixes * Fixed ``RequestDataTooBig`` and ``TooManyFieldsSent`` exceptions crashing rather than generating a bad request response (:ticket:`27820`). + +* Fixed a crash on Oracle and PostgreSQL when subtracting ``DurationField`` + or ``IntegerField`` from ``DateField`` (:ticket:`27828`). diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py index 0a414942178..5b754c88f1e 100644 --- a/tests/expressions/tests.py +++ b/tests/expressions/tests.py @@ -1184,6 +1184,12 @@ class FTimeDeltaTests(TestCase): ).order_by('name') self.assertQuerysetEqual(over_estimate, ['e3', 'e4'], lambda e: e.name) + def test_date_minus_duration(self): + more_than_4_days = Experiment.objects.filter( + assigned__lt=F('completed') - Value(datetime.timedelta(days=4), output_field=models.DurationField()) + ) + self.assertQuerysetEqual(more_than_4_days, ['e3', 'e4'], lambda e: e.name) + def test_negative_timedelta_update(self): # subtract 30 seconds, 30 minutes, 2 hours and 2 days experiments = Experiment.objects.filter(name='e0').annotate(