Opened 9 years ago

Last modified 8 years ago

#24959 closed Bug

date_interval_sql Implementations for Database Backends Do Not Handle Negative timedelta Objects Properly — at Initial Version

Reported by: Fred Palmer Owned by: nobody
Component: Database layer (models, ORM) Version: 1.8
Severity: Normal Keywords: date_interval_sql, timedelta, F, orm
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If I have a timedelta object in python that represents a negative difference, e.g.:

delta = timedelta(seconds=-3600)
print delta2
 -1 day, 23:00:00

The resultant SQL generated by date_interval_sql for the MySQL backend would be something like:

UPDATE `scheduling_event`
SET ...
        `my_datetime` = (`my_table`.`my_datetime` + INTERVAL '-1 0:0:82800:0' DAY_MICROSECOND),
WHERE (...)

AND what we want is the following:

UPDATE `scheduling_event`
SET  ...
`my_datetime` = (`my_table`.`my_datetime` + INTERVAL '-0 0:0:3600:0' DAY_MICROSECOND),
WHERE (...)

In layman's terms - the two layers are not convertible in a one-to-one sense. A timedelta in for the example above in Python means: go back one day and *add* 23 hours. So some datetime + delta would just subtract one hour.

In MySQL, however, "INTERVAL '-1 0:0:82800:0' DAY_MICROSECOND)" means: add a negative one day and 23 hours.

Change History (0)

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