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.
--
Ticket URL: <https://code.djangoproject.com/ticket/27828>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* owner: nobody => Vytis Banaitis
* status: new => assigned
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/8057 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/27828#comment:1>
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/27828#comment:2>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"d5088f838d837fc9e3109c828f18511055f20bea" d5088f83]:
{{{
#!CommitTicketReference repository=""
revision="d5088f838d837fc9e3109c828f18511055f20bea"
Fixed #27828 -- Fixed a crash when subtracting Integer/DurationField from
DateField on Oracle/PostgreSQL.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27828#comment:3>
Comment (by Tim Graham <timograham@…>):
In [changeset:"eedf276ed1b247c87d3852db4241da1aa9779589" eedf276e]:
{{{
#!CommitTicketReference repository=""
revision="eedf276ed1b247c87d3852db4241da1aa9779589"
[1.11.x] Fixed #27828 -- Fixed a crash when subtracting
Integer/DurationField from DateField on Oracle/PostgreSQL.
Backport of d5088f838d837fc9e3109c828f18511055f20bea from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27828#comment:4>
Comment (by Tim Graham <timograham@…>):
In [changeset:"75327b88a829e2c4be1775b24e0cf6349b26ace4" 75327b88]:
{{{
#!CommitTicketReference repository=""
revision="75327b88a829e2c4be1775b24e0cf6349b26ace4"
[1.10.x] Fixed #27828 -- Fixed a crash when subtracting
Integer/DurationField from DateField on Oracle/PostgreSQL.
Thanks Mariusz Felisiak for the Oracle workaround.
Backport of d5088f838d837fc9e3109c828f18511055f20bea from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27828#comment:5>