Opened 10 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 Version 3

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 (last modified by Fred Palmer)

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 `my_table`
SET ...
        `my_datetime` = (`my_table`.`my_datetime` + INTERVAL '-1 0:0:82800:0' DAY_MICROSECOND),
WHERE (...)

AND what we want is the following:

UPDATE `my_table`
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 (3)

comment:1 by Fred Palmer, 10 years ago

Description: modified (diff)

comment:2 by Fred Palmer, 10 years ago

Description: modified (diff)

comment:3 by Fred Palmer, 10 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top