#25177: Filter reference field on model to a date difference raise a TypeError
-------------------------------------+-------------------------------------
     Reporter:  Mounir               |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  1.8
  (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 Mariusz Felisiak):

 Replying to [comment:2 Josh Smeaton]:
 > This ticket seems really familiar, but I can't seem to find a duplicate
 at the moment. I'll keep hunting for it. In the mean time, there's a
 workaround for what you want to do:
 >
 > {{{
 >
 
Product.objects.annotate(days=ExpressionWrapper(F('start_date')-F('signed_date'),
 output_field=DurationField())).filter(days__lte=2)
 > }}}
 >
 > That works on Postgres at least, I'm not sure how other backends handle
 it.
 >
 > I think `Combinable._combine()` may need special handling for `date -
 date` producing durations though.

 This issue is not about combined expressions and LHS of a lookup, it's
 about RHS. Django calls `get_db_prep_lookup()` on LHS values when it's
 defined for the LHS output field. Currently the following example raises
 `AttributeError` on SQLite and MySQL, and `ProgrammingError` on PostgreSQL
 and Oracle (where `DurationField.get_db_prep_lookup()` is no-op).
 {{{
 Experiment.objects.annotate(days=F('start')-F('end')).filter(days__lte=11)
 }}}
 - SQLite and MySQL:
 {{{
   File "/home/felixx/repo/django/django/utils/duration.py", line 46, in
 duration_microseconds
     return (24 * 60 * 60 * delta.days + delta.seconds) * 1000000 +
 delta.microseconds
                            ^^^^^^^^^^
 AttributeError: 'int' object has no attribute 'days'
 }}}
 - Oracle and PostgreSQL:
 {{{
 django.db.utils.ProgrammingError: operator does not exist: interval <=
 integer
 LINE 1: ...art" - "expressions_ExPeRiMeNt"."end"))::interval <= 11 ORDE...
 }}}

 but it works fine when RHS is `timedelta` on all databases:
 {{{
 
Experiment.objects.annotate(days=F('start')-F('end')).filter(days__lte=datetime.timedelta(11))
 }}}

 I'm not sure if there is anything to fix here.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/25177#comment:3>
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 visit 
https://groups.google.com/d/msgid/django-updates/0107019a746c8d1f-4625b702-9584-44f5-a128-7180846c7a65-000000%40eu-central-1.amazonses.com.

Reply via email to