﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
27828	ORM crash on F('date_field') - F('int_field') on PostgreSQL	Vytis Banaitis	Vytis Banaitis	"PostgreSQL allows to use subtraction with a date and an int, returning a date. The integer is taken as a time delta in days.

I was trying to use this in Django.

{{{#!python
class DateModel(models.Model):
    date_field = models.DateField()
    int_field = models.IntegerField()
}}}

{{{#!python
In [3]: qs = DateModel.objects.annotate(result=ExpressionWrapper(F('date_field') - F('int_field'), output_field=DateField()))
}}}

However, this query crashes:
{{{#!python
In [4]: qs.all()
Out[4]: ---------------------------------------------------------------------------
ProgrammingError                          Traceback (most recent call last)
/home/vytis/src/django/django/db/backends/utils.py in execute(self, sql, params)
     61             else:
---> 62                 return self.cursor.execute(sql, params)
     63 

ProgrammingError: function age(date, integer) does not exist
LINE 1: ...del"".""date_field"", ""myapp_datemodel"".""int_field"", age(""myapp...
                                                             ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
}}}

Generated SQL:
{{{
SELECT ""myapp_datemodel"".""id"", ""myapp_datemodel"".""date_field"", ""myapp_datemodel"".""int_field"", age(""myapp_datemodel"".""date_field"", ""myapp_datemodel"".""int_field"") AS ""result"" FROM ""myapp_datemodel""
}}}

While looking for a solution, I came across a typo in django/db/models/expressions.py (introduced in [changeset:""766afc22a1dfa7d34a08de85356b7bc9dba025e7"" 766afc22]):
https://github.com/django/django/blob/9718fa2e8abe430c3526a9278dd976443d4ae3c6/django/db/models/expressions.py#L386
Note the `lhs_output` on both sides of operation.

Fixing this typo resolves the crash."	Bug	closed	Database layer (models, ORM)	1.10	Normal	fixed			Accepted	1	0	0	0	0	0
