Opened 17 years ago

Closed 14 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: dev
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

Description

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/models.py", line 472, in save
        super(MxEvent, self).save()
      File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/db/models/base.py", line 251, in save
        ','.join(placeholders)), db_values)
      File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/db/backends/util.py", line 19, in execute
        return self.cursor.execute(sql, params)
      File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/MySQLdb/cursors.py", 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/cursors.py", line 82, in _warning_check
        warn(w[-1], self.Warning, 3)
      File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/warnings.py", line 62, in warn
        globals)
      File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/warnings.py", 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 by Simon G. <dev@…>, 17 years ago

Triage Stage: UnreviewedDesign decision needed

comment:2 by clint, 17 years ago

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...)
new_post.save()

comment:3 by anonymous, 16 years ago

Cc: oliver@… added

comment:4 by Malcolm Tredinnick, 14 years ago

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