Opened 12 years ago

Closed 9 years ago

#5304 closed (invalid)

timezone aware datetime causes error when saving on mysql

Reported by: jvisinand@… Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords: timezone aware datetime causes error when saving on mysql
Cc: oliver@… Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


It appears that the mysql database engine doesn't support timezone aware datetimes. Although I always save datetime with the timezone set to the TIME_ZONE settings, I have an error. It works on Postgres.

Way to reproduce it:

  • Create a Model with a DateTimeField field
  • Create an object and set this field to a timezone aware datetime
  • Save it:
    Traceback (most recent call last):
      File "<console>", line 1, in <module>
      File "/Volumes/MacData/Users/juju/mixin/events/", line 472, in save
        super(MxEvent, self).save()
      File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/db/models/", line 251, in save
        ','.join(placeholders)), db_values)
      File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/db/backends/", line 19, in execute
        return self.cursor.execute(sql, params)
      File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/MySQLdb/", line 168, in execute
        if not self._defer_warnings: self._warning_check()
      File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/MySQLdb/", line 82, in _warning_check
        warn(w[-1], self.Warning, 3)
      File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/", line 62, in warn
      File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/", line 102, in warn_explicit
        raise message
    Warning: Out of range value for column 'start_date' at row 1

Work around: Save non timezone aware datetime.

Change History (4)

comment:1 Changed 12 years ago by Simon G. <dev@…>

Triage Stage: UnreviewedDesign decision needed

comment:2 Changed 11 years ago by clint

I got around this in my code by converting my datetimes to naive before using them to create a model. ala:

date_posted = date_posted.replace(tzinfo=None)
new_post = Post(title=..., date_posted=date_posted...)

comment:3 Changed 11 years ago by anonymous

Cc: oliver@… added

comment:4 Changed 9 years ago by Malcolm Tredinnick

Resolution: invalid
Status: newclosed

This is correct behaviour. Django cannot save that data and so an error is raised. We cannot work around that (without throwing away information), so have to stop there.

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