Django-migrate-Error:`ValueError:基数为10的int()的无效文字:''

时间:2018-04-05 23:55:14

标签: python django django-models django-migrations

我是django-beginner,我不理解这个错误,也不理解其他帖子的解释:ValueError: invalid literal for int() with base 10: ''

我该如何解决? 我的追溯:

    PS C:\Users\Tobi\Desktop\LeftLife\instapic> python manage.py migrate
System check identified some issues:

WARNINGS:
?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'
        HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/2.0/ref/databases/#mysql-sql-mode
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, instapic, sessions
Running migrations:
  Applying instapic.0012_auto_20180406_0115...Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\commands\migrate.py", line 200, in handle
    fake_initial=fake_initial,
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\migrations\migration.py", line 122, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards
    field,
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\mysql\schema.py", line 40, in add_field
    super().add_field(model, field)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\base\schema.py", line 407, in add_field
    definition, params = self.column_sql(model, field, include_default=True)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\base\schema.py", line 152, in column_sql
    default_value = self.effective_default(field)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\base\schema.py", line 224, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\models\fields\__init__.py", line 767, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\models\fields\__init__.py", line 762, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\models\fields\__init__.py", line 1826, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: ''

    PS C:\Users\Tobi\Desktop\LeftLife\instapic> python manage.py migrate
    System check identified some issues:

    WARNINGS:
    ?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'
            HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/2.0/ref/databases/#mysql-sql-mode
    Operations to perform:
      Apply all migrations: admin, auth, contenttypes, instapic, sessions
    Running migrations:
      Applying instapic.0012_auto_20180406_0115...Traceback (most recent call last):
      File "manage.py", line 22, in <module>
        execute_from_command_line(sys.argv)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
        utility.execute()
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\__init__.py", line 365, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
        self.execute(*args, **cmd_options)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\base.py", line 335, in execute
        output = self.handle(*args, **options)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\commands\migrate.py", line 200, in handle
        fake_initial=fake_initial,
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
        state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
        state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
        state = migration.apply(state, schema_editor)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\migrations\migration.py", line 122, in apply
        operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards
        field,
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\mysql\schema.py", line 40, in add_field
        super().add_field(model, field)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\base\schema.py", line 407, in add_field
        definition, params = self.column_sql(model, field, include_default=True)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\base\schema.py", line 152, in column_sql
        default_value = self.effective_default(field)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\base\schema.py", line 224, in effective_default
        default = field.get_db_prep_save(default, self.connection)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\models\fields\__init__.py", line 767, in get_db_prep_save
        return self.get_db_prep_value(value, connection=connection, prepared=False)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\models\fields\__init__.py", line 762, in get_db_prep_value
        value = self.get_prep_value(value)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\models\fields\__init__.py", line 1826, in get_prep_value
        return int(value)
    ValueError: invalid literal for int() with base 10: ''

我的模特是:

    PS C:\Users\Tobi\Desktop\LeftLife\instapic> python manage.py migrate
    System check identified some issues:

    WARNINGS:
    ?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'
            HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/2.0/ref/databases/#mysql-sql-mode
    Operations to perform:
      Apply all migrations: admin, auth, contenttypes, instapic, sessions
    Running migrations:
      Applying instapic.0012_auto_20180406_0115...Traceback (most recent call last):
      File "manage.py", line 22, in <module>
        execute_from_command_line(sys.argv)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
        utility.execute()
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\__init__.py", line 365, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
        self.execute(*args, **cmd_options)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\base.py", line 335, in execute
        output = self.handle(*args, **options)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\commands\migrate.py", line 200, in handle
        fake_initial=fake_initial,
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
        state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
        state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
        state = migration.apply(state, schema_editor)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\migrations\migration.py", line 122, in apply
        operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards
        field,
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\mysql\schema.py", line 40, in add_field
        super().add_field(model, field)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\base\schema.py", line 407, in add_field
        definition, params = self.column_sql(model, field, include_default=True)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\base\schema.py", line 152, in column_sql
        default_value = self.effective_default(field)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\base\schema.py", line 224, in effective_default
        default = field.get_db_prep_save(default, self.connection)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\models\fields\__init__.py", line 767, in get_db_prep_save
        return self.get_db_prep_value(value, connection=connection, prepared=False)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\models\fields\__init__.py", line 762, in get_db_prep_value
        value = self.get_prep_value(value)
      File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\models\fields\__init__.py", line 1826, in get_prep_value
        return int(value)
    ValueError: invalid literal for int() with base 10: ''

1 个答案:

答案 0 :(得分:0)

你可以在django中强制执行严格的sql模式

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'OPTIONS': {
            'sql_mode': 'traditional',
        }
    }
}