Code

Opened 7 years ago

Closed 7 years ago

#4954 closed (wontfix)

sqlite3 time fields can fail typecasting

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

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

Attachments (0)

Change History (1)

comment:1 Changed 7 years ago by Matt McClanahan <cardinal@…>

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to wontfix
  • Status changed from new to closed
  • Summary changed from problems with sqlite3 to sqlite3 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.