#24959: date_interval_sql Implementations for Database Backends Do Not Handle Negative timedelta Objects Properly -------------------------------------+------------------------------------- Reporter: fredpalmer | Owner: nobody Type: Uncategorized | Status: new Component: Database layer | Version: 1.8 (models, ORM) | Severity: Normal | Resolution: Keywords: date_interval_sql, | Triage Stage: timedelta, F, orm | Unreviewed Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+------------------------------------- Description changed by fredpalmer:
Old 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 `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'''. New 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 `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'''. -- -- Ticket URL: <https://code.djangoproject.com/ticket/24959#comment:2> 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 django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/068.cdcd185c78e8ea7b413a5c9590a45d83%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.