Reputation: 18
I'm getting a value error, when I'm trying to add new data to my database.
My models.py:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User
DEFAULT_ROLE_ID=1
class UserProfile(models.Model):
user= models.OneToOneField(User)
avatar= models.ImageField(upload_to='Images/users', verbose_name='Image')
rating=models.IntegerField(default=0)
karma=models.IntegerField(default=0)
def __unicode__(self):
return unicode(self.user)
class Room(models.Model):
title=models.CharField(max_length=63,default='test')
time_creation=models.DateTimeField('Time of room creation')
users=models.ManyToManyField(UserProfile)
def __unicode__(self):
return unicode(self.title)
class Game(models.Model):
title=models.CharField(max_length=63,default='test')
time_creation=models.DateTimeField('Время создания')
room=models.ForeignKey(Room)
def __unicode__(self):
return unicode(self.title)
class SecretWord(models.Model):
word=models.CharField(max_length=255)
game=models.ForeignKey(Game)
def __unicode__(self):
return unicode(self.word)
class UserRole(models.Model):
PLAYER = 'PL'
LIDER = 'LID'
ROLE_CHOICES = (
(LIDER, 'Lider'),
(PLAYER, 'Player'),
)
user=models.ForeignKey(UserProfile,default=1)
game=models.ForeignKey(Game,default=1)
role = models.CharField(max_length=3,choices=ROLE_CHOICES,default=LIDER)
def __unicode__(self):
return unicode(self.role)
class Message(models.Model):
text=models.TextField(max_length=2047)
time_creation=models.DateTimeField('Time of room creation')
room=models.ForeignKey(Room)
user=models.ForeignKey(UserProfile)
user_role=models.ForeignKey(UserRole, default=DEFAULT_ROLE_ID)
def __unicode__(self):
return unicode(self.text)
The error is:
Applying hat.0030_auto_20160421_1632...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 200, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 92, in migrate
self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 123, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
field,
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/backends/mysql/schema.py", line 50, in add_field
super(DatabaseSchemaEditor, self).add_field(model, field)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 382, in add_field
definition, params = self.column_sql(model, field, include_default=True)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 145, in column_sql
default_value = self.effective_default(field)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 210, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 915, in get_db_prep_save
return self.target_field.get_db_prep_save(value, connection=connection)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 728, in get_db_prep_save
prepared=False)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 968, in get_db_prep_value
value = self.get_prep_value(value)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 976, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'Player'
I have tried to comment out different parts of code, but didn't even understand in which exactly place, where I use data from UserRole model, I made a mistake.
P.S.And I just noticed: after I change my code and using 'python manage.py migrate' I get this error:
Operations to perform:
Apply all migrations: admin, contenttypes, hat, auth, sessions
Running migrations:
Applying hat.0030_auto_20160421_1632...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 200, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 92, in migrate
self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 123, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/migrations/operations/models.py", line 59, in database_forwards
schema_editor.create_model(model)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 284, in create_model
self.execute(sql, params or None)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 110, in execute
cursor.execute(sql, params)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 112, in execute
return self.cursor.execute(query, args)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.OperationalError: (1050, "Table 'hat_usergame' already exists")
But the table 'usergame' has another name - 'UserRole' now. I have so habituated to this error, that even don't noticed that. I usually use command 'python manage.py reset_db' from django-extensions and everything works after it. As I understand, I should delete old migrations, shouldn't I? Maybe it can fix up my another error?
I'm a novice in Django, so maybe I simply don't understand elementary things.
In result the problem was solved by deleting migrations, beginning with migration number 0030, when everything went wrong.
Viewing list of migrations:
python manage.py migrate --list
app
(*) 0001_initial
(*) 0002_auto__...
(*) 0003_auto__...
(*) 0004_auto__...
Applying the last correct migration and deleting the following:
./manage.py migrate app 0003
rm app/migrations/0004*
Upvotes: 0
Views: 1763
Reputation: 1015
So, one thing is for sure. The error that you are getting while running migrations, is because django is reading one of the files that makemigrations would have made and in which, there would have been 'usergame' table. Though, retracting migrations could be a pain sometimes (deleting old migrations file is one way), but by running reset_db command, you are effectively dropping the db and recreating it. What, I would generally do in this case is, first drop the db, then run makemigrations and then run migrations. But, in this case, run makemigrations only when you are sure about your models. Finalize your models, run python manage.py makemigrations and subsequently migrate command.
Secondly, you wrote in your second paragraph, that after you changed your code and ran migrations, you got the error. Now, since looking at your (modified) models and relating the error that you got (invalid literal), it's a bit difficult to understand the reason behind your error (because your model seems fine). The best possible theory, that I came up with was: since ValueError: invalid literal for int() with base 10
comes when in a field which accepts integers and and in that field, you are supplying floats. So, either convert that field to FloatField or supply correct values in that field (I am sure you must be aware of this fact, but just writing just in case: even a 8.0000 is not an int, it's a float).
Take a shot at retracting your migrations and possibly, when the new models get into your db, hopefully the problem might resolve.
Upvotes: 1