Reputation: 66
I have a big issue here, I just cannot migrate my database as I want. I test a lot of things (even write directly in database...). It runs with Django 1.7.1 and my database is store with Sqlite3. So here's my issue:
I have a model like this:
from django.db import models
import datetime
class UserUsingTime(models.Model):
userid = models.CharField(max_length = 30)
using_time = models.DateTimeField(default = datetime.time())
(The thing that I want to do is to store a timer. Like maybe all times in a race for example.)
And I add this line at the end of my model:
app_name = models.CharField(max_length = 20, null = True)
Then I run python manage makemigrations my_app_name
and it results:
Migrations for 'my_app_name':
0003_userusingtime_app_name.py:
- Add field app_name to userusingtime
So for now all is good :) But when I try to run python manage migrate
then I have tons of errors lines stuff:
Operations to perform:
Apply all migrations: admin, contenttypes, my_app_name, auth, sessions
Running migrations:
Applying my_app_name.0003_userusingtime_app_name...Traceback (most recent call last):
File "./manage.py", line 8, in
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 160, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 63, in migrate
self.apply_migration(migration, fake=fake)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 97, in apply_migration
migration.apply(project_state, schema_editor)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 107, in apply
operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/fields.py", line 37, in database_forwards
field,
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/schema.py", line 167, in add_field
self._remake_table(model, create_fields=[field])
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/schema.py", line 128, in _remake_table
self.create_model(temp_model)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/schema.py", line 209, in create_model
definition, extra_params = self.column_sql(model, field)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/schema.py", line 121, in column_sql
default_value = self.effective_default(field)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/schema.py", line 184, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 627, in get_db_prep_save
prepared=False)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1286, in get_db_prep_value
value = self.get_prep_value(value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1269, in get_prep_value
value = super(DateTimeField, self).get_prep_value(value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1171, in get_prep_value
return self.to_python(value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1228, in to_python
parsed = parse_datetime(value)
File "/usr/local/lib/python2.7/dist-packages/django/utils/dateparse.py", line 70, in parse_datetime
match = datetime_re.match(value)
TypeError: expected string or buffer
So I really don't know what to do next... Plus it seems there is a datetime
errors and I can't explain that because I did not touch any datetime
object in my models... So weird!
Please help me out with this issue :) I thank you in advance guys (Y) !
Upvotes: 1
Views: 1568
Reputation: 48952
Your using_time
field definition is faulty. Django is complaining because you're trying to store a time
object in a datetime
container.
DateTimeField
is simply the wrong kind of field for what you're trying to represent (a length of time). You could switch this to a TimeField
, or simply use a FloatField
or IntegerField
to store the seconds or milliseconds.
(Why is this only throwing a migrations error now? I assume it's because this bug wasn't caught in an earlier version of Django, and this is the first real migration (i.e. not the initial, fake migration) that you've tried in 1.7.)
Upvotes: 1