Opened 17 years ago

Closed 17 years ago

#4954 closed (wontfix)

sqlite3 time fields can fail typecasting

Reported by: anonymous Owned by: Adrian Holovaty
Component: Database layer (models, ORM) Version: dev
Severity: Keywords: sqlite3 timefield
Cc: sql@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

My model (excerpt):

class TimeClock(models.Model):
    user = models.ForeignKey(User)
    day = models.DateField()
    start_time = models.TimeField()
    end_time = models.TimeField(blank=True, null=True)
    pause = models.IntegerField(help_text="In Minutes", blank=True, default=0)

The error:

Traceback (most recent call last):
File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py" in get_response
  77. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/views/decorators.py" in _checklogin
  55. return view_func(request, *args, **kwargs)
File "/usr/lib/python2.5/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  39. response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/views/main.py" in change_list
  772. cl = ChangeList(request, model)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/views/main.py" in __init__
  578. self.get_results(request)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/views/main.py" in get_results
  636. result_list = list(self.query_set)
File "/usr/lib/python2.5/site-packages/django/db/models/query.py" in __iter__
  113. return iter(self._get_data())
File "/usr/lib/python2.5/site-packages/django/db/models/query.py" in _get_data
  480. self._result_cache = list(self.iterator())
File "/usr/lib/python2.5/site-packages/django/db/models/query.py" in iterator
  188. cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params)
File "/usr/lib/python2.5/site-packages/django/db/backends/util.py" in execute
  19. return self.cursor.execute(sql, params)
File "/usr/lib/python2.5/site-packages/django/db/backends/sqlite3/base.py" in execute
  94. return Database.Cursor.execute(self, query, params)
File "/usr/lib/python2.5/site-packages/django/db/backends/util.py" in typecast_time
  68. return datetime.time(int(hour), int(minutes), int(seconds), int(float('.'+microseconds) * 1000000))

  ValueError at /admin/timeclock/timeclock/
  invalid literal for int() with base 10: '2007-07-22 21'

It occurs viewing the admin list view, after inserting an object like this:
new = TimeClock(user=request.user,day=now,start_time=now )
where now = datetime.datetime.now()

In sqlite3 it looks like this:

sqlite> select * from timeclock_timeclock;
1|1|2007-07-22|2007-07-22 21:48:17.782227||0

Change History (1)

comment:1 by Matt McClanahan <cardinal@…>, 17 years ago

Resolution: wontfix
Status: newclosed
Summary: problems with sqlite3sqlite3 time fields can fail typecasting

This appears to be a sqlite issue stemming from storing date and time fields as strings. Other DB backends generally let you populate a time field with a date-time, but drop the date component. You can work around this issue by using datetime.datetime.now().time() instead of datetime.datetime.now().

Anything Django might attempt to do to work around this is error-prone. The sqlite backend could pattern match values that come out of time fields, trimming off bits that aren't a time string, but if a model with such a field were read from the DB and saved, the time field would be modified. While this might not be seen as a bad thing, it is at least questionable to passively sanitize bad data for the user.

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