Django manage.py 测试:“数据库后端不接受 0 作为 AutoField 的值”(mysql)
Posted
技术标签:
【中文标题】Django manage.py 测试:“数据库后端不接受 0 作为 AutoField 的值”(mysql)【英文标题】:Django manage.py test: “database backend does not accept 0 as a value for AutoField” (mysql) 【发布时间】:2019-07-19 07:56:31 【问题描述】:我正在尝试使用以下方法运行 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.
如何修复此错误并运行测试?
【问题讨论】:
好吧,根据回溯,它确实发生在迁移期间:测试数据库的迁移。 @WillemVanOnsem 谢谢,这确实是真的,但是对于测试迁移我能做些什么呢?他们在迁移文件夹中没有迁移文件。 看看这个:***.com/a/46402659/6808714 @MahrezBenHamad 谢谢,我在发布之前已经看过它,但我花了一段时间才意识到这些是切换到 MTPP 引起的迁移问题,并设置了 lft、level 等解决方法......为 0,然后删除这些字段。我最终删除了迁移,因为这是一个测试项目。 【参考方案1】:好吧,看来这确实是 @Willem Van Onsem 建议的迁移。我最终删除了迁移并解决了问题。
可能值得指出的是,混乱是由于从 Django 模型切换到 Django MTPP 模型并使用0
作为新添加的 MTPP 字段的默认值以避免该过程中的另一个问题造成的。
【讨论】:
以上是关于Django manage.py 测试:“数据库后端不接受 0 作为 AutoField 的值”(mysql)的主要内容,如果未能解决你的问题,请参考以下文章
如何查看在 Django 的 manage.py test 命令期间运行了哪些测试
如何直接从测试驱动程序调用自定义 Django manage.py 命令?
python测试开发django-176.数据库迁移数据(manage.py dumpdata)
python测试开发django-176.数据库迁移数据(manage.py dumpdata)
./manage.py 对 Django 1.4 的测试给出了在 google appengine 上找不到的 Thing2Literal 导入