Django manage.py测试:“数据库后端不接受0作为AutoField的值”(mysql)

时间:2019-02-25 20:33:43

标签: python django django-testing django-migrations django-tests

我正在尝试使用以下命令运行Django测试文件:

python3.6 manage.py test

我使用: MySQL 5.5.62 Python 3.6 Django 2.0.0

它开始安装测试数据库,并失败,并显示以下错误:

ValueError: The database backend does not accept 0 as a value for AutoField.

我搜索了此错误,但是我发现的所有主题都与迁移相关,例如this one。我的迁移没有问题,它们运行平稳。

跟踪:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/test.py", line 26, in run_from_argv
    super().run_from_argv(argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/test.py", line 59, in handle
    failures = test_runner.run_tests(test_labels)
  File "/usr/local/lib/python3.6/site-packages/django/test/runner.py", line 601, in run_tests
    old_config = self.setup_databases()
  File "/usr/local/lib/python3.6/site-packages/django/test/runner.py", line 548, in setup_databases
    self.parallel, **kwargs
  File "/usr/local/lib/python3.6/site-packages/django/test/utils.py", line 176, in setup_databases
    serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True),
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/creation.py", line 68, in create_test_db
    run_syncdb=True,
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 141, in call_command
    return command.execute(*args, **defaults)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    fake_initial=fake_initial,
  File "/usr/local/lib/python3.6/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 "/usr/local/lib/python3.6/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 "/usr/local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/migration.py", line 122, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 216, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 509, in alter_field
    old_db_params, new_db_params, strict)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 613, in _alter_field
    old_default = self.effective_default(old_field)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 224, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "/usr/local/lib/python3.6/site-packages/django/db/models/fields/related.py", line 936, in get_db_prep_save
    return self.target_field.get_db_prep_save(value, connection=connection)
  File "/usr/local/lib/python3.6/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 "/usr/local/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 940, in get_db_prep_value
    value = connection.ops.validate_autopk_value(value)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/operations.py", line 163, in validate_autopk_value
    raise ValueError('The database backend does not accept 0 as a '
ValueError: The database backend does not accept 0 as a value for AutoField.

如何解决此错误并运行测试?

1 个答案:

答案 0 :(得分:0)

好的,看来确实确实是@Willem Van Onsem所建议的迁移。我最终删除了迁移,这消除了问题。

可能值得指出的是,混乱是由于从Django模型切换到Django MTPP模型,并使用var x = document.getElementById("button"); x.addEventListener("click", resolve); var y = parseInt(document.getElementById("x").value); function resolve() { if (y >= 20000) { alert("yes greater than 20000"); } else { alert("no good"); } } 作为新添加的MTPP字段的默认值来避免此过程中的另一个问题。