#31506: ExpressionWrapper() doesn't respect output_field when combining 
DateField
and timedelta on PostgreSQL and MySQL.
-------------------------------------+-------------------------------------
     Reporter:  Matthieu Rigal       |                    Owner:  (none)
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Matthieu Rigal):

 Replying to [comment:6 David Sanders]:
 > There are a few ways to address this but IMHO I don't think anything
 should be done here as this is documented PostgreSQL behaviour [1]
 >
 > Therefore I'd recommend this ticket be marked `wontfix`.
 >
 > I think it's the developers responsibility to cast in this situation
 instead of using `ExpressionWrapper`:
 >
 > {{{
 > next_segments = StartModel.objects.filter('start__gt':
 OuterRef('start')).order_by('start')
 > qs = StartModel.objects.annotate(
 >             end=Cast(
 >                 Subquery(next_segments.values('start')[:1]) -
 datetime.timedelta(days=1),
 >                 output_field=DateField(),
 >             )
 >         )
 > }}}
 >
 >
 > [1] https://www.postgresql.org/docs/current/functions-datetime.html
 #OPERATORS-DATETIME-TABLE

 Thanks David!
 Seeing it from a PostgreSQL perspective, it makes sense, as interval has
 the precision of a timestamp!
 However, as this is pretty surprising from a Pythonic point-of-view, I
 would rather consider extending the documentation to warn about this
 potential trap. It should be clear that `timedelta` can only be cast (by
 Psycopg2) to an interval and therefore does not allow the use of the `date
 +/- integer → date` operation of PostgreSQL.

 Besides the example with `Cast` one may add that in case this patterns is
 recurrent, it may be worth adding that creating a custom Field `DateDelta`
 translating to integer on PostrgeSQL side, could be an option...

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31506#comment:9>
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018378ff4ea3-edfab130-e5ca-46fd-9d98-8647e78f7d6e-000000%40eu-central-1.amazonses.com.

Reply via email to