Opened 4 years ago

Closed 4 years ago

#31446 closed Bug (invalid)

Annotate with ExpressionWrapper gives wrong result on date field.

Reported by: Gerben Morsink Owned by: nobody
Component: Database layer (models, ORM) Version: 2.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Gerben Morsink)

When doing:

from django.db.models import DateField, ExpressionWrapper, F

Ticket.objects.annotate(
    notify_date=ExpressionWrapper(
        F('date') - F('notify_before_in_days'), output_field=DateField()))

With on Ticket a modelfield: date = models.DateField() will yield wrong results if notify_before_in_days passes through the month boundary.
E.g. If date is 9th of April and notify_before_in_days = 10, it will yield: 20200399 for notify_date, instead of 20200331.

Database is MySQL 8.0.19-0ubuntu0.19.10.3 - (Ubuntu), Django version is 2.2.10.

Change History (4)

comment:1 by Gerben Morsink, 4 years ago

Description: modified (diff)

comment:2 by Gerben Morsink, 4 years ago

Description: modified (diff)

comment:3 by Gerben Morsink, 4 years ago

Description: modified (diff)

comment:4 by Mariusz Felisiak, 4 years ago

Resolution: invalid
Status: newclosed
Summary: Annotate with ExpressionWrapper gives wrong result on date fieldAnnotate with ExpressionWrapper gives wrong result on date field.

I think you mixed field types. You cannot subtract an IntegerField from the DateField because MySQL will implicitly cast a date to integer, as a result you'll get 20200308999990.000000. Using DurationField works as expected and returns 2020-02-28 00:00:00.

Closing per TicketClosingReasons/UseSupportChannels.

Note: See TracTickets for help on using tickets.
Back to Top