#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/049.433349467209f749f240b55df1055acb%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to