Opened 5 years ago
Closed 5 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 )
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 , 5 years ago
Description: | modified (diff) |
---|
comment:2 by , 5 years ago
Description: | modified (diff) |
---|
comment:3 by , 5 years ago
Description: | modified (diff) |
---|
comment:4 by , 5 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
Summary: | Annotate with ExpressionWrapper gives wrong result on date field → Annotate with ExpressionWrapper gives wrong result on date field. |
Note:
See TracTickets
for help on using tickets.
I think you mixed field types. You cannot subtract an
IntegerField
from theDateField
because MySQL will implicitly cast a date to integer, as a result you'll get20200308999990.000000
. UsingDurationField
works as expected and returns2020-02-28 00:00:00
.Closing per TicketClosingReasons/UseSupportChannels.