[Django] #27828: ORM crash on F('date_field') - F('int_field') on PostgreSQL

14 views
Skip to first unread message

Django

unread,
Feb 12, 2017, 1:19:17 PM2/12/17
to django-...@googlegroups.com
#27828: ORM crash on F('date_field') - F('int_field') on PostgreSQL
-------------------------------------+-------------------------------------
Reporter: Vytis | Owner: nobody
Banaitis |
Type: Bug | Status: new
Component: Database | Version: 1.10
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/27828>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 12, 2017, 1:25:30 PM2/12/17
to django-...@googlegroups.com
#27828: ORM crash on F('date_field') - F('int_field') on PostgreSQL
-------------------------------------+-------------------------------------
Reporter: Vytis Banaitis | Owner: Vytis
| Banaitis
Type: Bug | Status: assigned
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Vytis Banaitis):

* 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>

Django

unread,
Feb 13, 2017, 7:31:07 AM2/13/17
to django-...@googlegroups.com
#27828: ORM crash on F('date_field') - F('int_field') on PostgreSQL
-------------------------------------+-------------------------------------
Reporter: Vytis Banaitis | Owner: Vytis
| Banaitis
Type: Bug | Status: assigned
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/27828#comment:2>

Django

unread,
Feb 16, 2017, 12:35:37 PM2/16/17
to django-...@googlegroups.com
#27828: ORM crash on F('date_field') - F('int_field') on PostgreSQL
-------------------------------------+-------------------------------------
Reporter: Vytis Banaitis | Owner: Vytis
| Banaitis
Type: Bug | Status: closed

Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* 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>

Django

unread,
Feb 16, 2017, 12:43:49 PM2/16/17
to django-...@googlegroups.com
#27828: ORM crash on F('date_field') - F('int_field') on PostgreSQL
-------------------------------------+-------------------------------------
Reporter: Vytis Banaitis | Owner: Vytis
| Banaitis
Type: Bug | Status: closed
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Feb 16, 2017, 5:28:32 PM2/16/17
to django-...@googlegroups.com
#27828: ORM crash on F('date_field') - F('int_field') on PostgreSQL
-------------------------------------+-------------------------------------
Reporter: Vytis Banaitis | Owner: Vytis
| Banaitis
Type: Bug | Status: closed
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Reply all
Reply to author
Forward
0 new messages