简体   繁体   中英

Django 1.7 ValueError: invalid literal for int() with base 10: 'a'

I am getting this error:

    Operations to perform:
  Apply all migrations: account, jobs, assets, sessions, admin, auth, laptops, contenttypes, mardes
Running migrations:
  Applying assets.0004_auto_20150202_1707...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    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/mysql/schema.py", line 42, in add_field
    super(DatabaseSchemaEditor, self).add_field(model, field)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/schema.py", line 397, in add_field
    definition, params = self.column_sql(model, field, include_default=True)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/schema.py", line 120, in column_sql
    default_value = self.effective_default(field)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/schema.py", line 183, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 1722, in get_db_prep_save
    return self.related_field.get_db_prep_save(value, connection=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 907, 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 915, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: 'a'

When running python manage.py migrate.

For this app there is only an admin.py and models.py file, everything else is blank. Here is the models.py file:

from django.db import models
from django.contrib.auth.models import User

class Asset_type(models.Model):
    name = models.CharField(max_length=50)

    def __str__(self):
        return self.name

class Asset_os(models.Model):
    name = models.CharField(max_length=50)

    def __str__(self):
        return self.name

class Asset_cs_status(models.Model):
    status = models.CharField(max_length=50)

    def __str__(self):
        return self.status

class Asset_floor(models.Model):
    name = models.CharField(max_length=50)

    def __str__(self):
        return self.name

class Asset(models.Model):
    DLO = (
            ('0', 'Desktop'),
            ('1', 'Laptop'),
            ('2', 'Other'),
            )
    user = models.ForeignKey(User)
    asset_type = models.ForeignKey(Asset_type)
    asset_os = models.ForeignKey(Asset_os)
    asset_cs_status = models.ForeignKey(Asset_cs_status)
    asset_floor = models.ForeignKey(Asset_floor)
    ppl = models.CharField(max_length=40, unique=True)
    desktop_laptop = models.CharField(max_length=1, choices=DLO) 
    date_of_purchase = models.DateField()
    extra_info = models.CharField(max_length=250, default='-')
    pc_name = models.CharField(max_length=100, null=True, default='-')
    bookable = models.BooleanField(default=False)
    image = models.ImageField(upload_to='asset_files/%Y/%m/%d', null=True, blank=True, help_text='Optional')

    def __str__(self):
        return self.asset_type.name+' ('+self.ppl+')' 

Please let me know if you need to see anything else. BC

The solution was found looking in the assets.0004_auto_20150202_1707 file under assets > migrations

It had picked up a default value I had typed in previously. Removing the files and regenerating them solved the problem.

Well, something migration fails due to Type mismatch.

eg IntegerField may or may not have default value '', something, django migration don't warning you in a consistent way. Give its default value -1 will pass error check in django..

so, general solution is:

  1. search type mismatch on default value

if not fixed, then

  1. delete model, by adding these magic lines:
 operations = [ migrations.DeleteModel("Your Model Name"), ] 
  1. run makemigration again.

This will drop the old database and create a new one without:

ask any goofy questions 10 times

Did you rename training.g1 to training.haha (a IntegerField)? [y/N]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM