我正在尝试运行我的应用程序测试(Django v1.11和Wagtail v2.2.1),但在测试创建测试数据库时出现异常:
Traceback (most recent call last): File "/home/fleon/.virtualenvs/virmyasb/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute return self.cursor.execute(sql, params) psycopg2.IntegrityError: null value in column "draft_title" violates not-null constraint DETAIL: Failing row contains (3, 0001, 1, 1, Root, root, t, f, /, , f, , null, null, f, 1, null, f, null, null, null, null, null).
在查看完整堆栈跟踪时,Wagtail文件中有一个错误:
[...] lib/python3.6/site-packages/wagtail/core/migrations/0001_squashed_0016_change_page_url_path_to_text_field.py", line 30, in initial_data [...]
这一行中的Python代码是:
# Create root page
root = Page.objects.create(
title="Root",
slug='root',
content_type=page_content_type,
path='0001',
depth=1,
numchild=1,
url_path='/',
) 这不是设置draft_title,因此违反了非空约束。
请注意,我使用的是两个不同的数据库(base.py):
DATABASES = {
'default': dj_database_url.config(),
'thedbname': {
'NAME': 'thedbname',
'ENGINE': 'django.db.backends.mysql',
'USER': 'thedbuser',
'PASSWORD': '***',
'HOST': 'myhost',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
},
}
}如果我删除MySQL数据库(?),测试就会工作。
发布于 2018-08-23 20:24:13
Page.draft_title字段被添加到迁移wagtail/core/migrations/0040_page_draft_title.py中的Wagtail。因为迁移总是在迁移时存在的模型的冻结版本上工作,所以0001_squashed_0016_change_page_url_path_to_text_field.py中的代码是有效的- draft_title当时并不存在。
这个错误表明,由于某些原因,迁移已经不按正常顺序发生了--我建议检查项目中任何可能影响顺序的依赖行,比如run_before --或者是在已经应用了迁移的数据库上重新运行迁移(但没有将它们记录在django_migrations表中,所以Django不知道这一点)。
https://stackoverflow.com/questions/51985358
复制相似问题