对于我的 Django 1.8.12 站点,我有一个example_content
固定装置,用于填充一些初始站点内容。
如果我在运行后立即加载migrate
它,它会很好地导入:
$ ./manage.py migrate --noinput
Operations to perform:
Synchronize unmigrated apps: ckeditor, staticfiles, zinnia_ckeditor, messages, djangocms_admin_style, webapp_creator, template_debug, sekizai, django_pygments, treebeard
Apply all migrations: djangocms_inherit, djangocms_snippet, api_docs, cmsplugin_zinnia, sites, menus, contenttypes, store_data, zinnia, django_comments, sessions, rev
ersion, auth, djangocms_picture, tagging, developer_portal, admin, djangocms_link, djangocms_video, django_openid_auth, md_importer, cms, djangocms_text_ckeditor
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
... (skipped for brevity).
Applying django_comments.0003_add_submit_date_index... OK
$ ./manage.py loaddata example_content
Installed 139582 object(s) from 1 fixture(s)
$
但是,我希望这些数据在 上自动导入migrate
,而不是需要额外的手动loaddata
步骤,所以我试图创建一个迁移my_app.0002_example_content
,来调用loaddata
命令。
我通过在每个相关模块上添加依赖项来确保首先加载所有其他数据。
为了确认此迁移确实是最后运行的(就像它在上面成功时一样),我首先注释掉了RunPython
将迁移留空的步骤:
# my_app/migrations/0002_example_content.py
from django.core.management import call_command
from django.db import models, migrations
class Migration(migrations.Migration):
from django.core.management import call_command
from django.db import models, migrations
dependencies = [
('djangocms_inherit', '0002_auto_20150622_1244'),
... all other apps (skipped for brevity)
('my_app', '0001_initial'),
]
operations = [
# migrations.RunPython(
# lambda apps, schema_editor: call_command("loaddata", "example_content")
# )
]
果然,它作为最后一次迁移运行:
$ ./manage.py migrate --noinput
Operations to perform:
Synchronize unmigrated apps: ckeditor, staticfiles, zinnia_ckeditor, messages, djangocms_admin_style, webapp_creator, template_debug, sekizai, django_pygments, treebeard
Apply all migrations: djangocms_inherit, djangocms_snippet, api_docs, cmsplugin_zinnia, sites, menus, contenttypes, store_data, zinnia, django_comments, sessions, reversion, auth, djangocms_picture, tagging, developer_portal, admin, djangocms_link, djangocms_video, django_openid_auth, md_importer, cms, djangocms_text_ckeditor
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
... (skipped for brevity)
Applying django_comments.0003_add_submit_date_index... OK
Applying my_app.0001_initial... OK
Applying my_app.0002_example_content... OK
$
但是,当我尝试运行它以实际运行loaddata
命令(重新注释代码)时,出现错误:
$ ./manage.py migrate --noinput
Operations to perform:
Synchronize unmigrated apps: ckeditor, staticfiles, zinnia_ckeditor, messages, djangocms_admin_style, webapp_creator, template_debug, sekizai, django_pygments, treebeard
Apply all migrations: djangocms_inherit, djangocms_snippet, api_docs, cmsplugin_zinnia, sites, menus, contenttypes, store_data, zinnia, django_comments, sessions, reversion, auth, djangocms_picture, tagging, developer_portal, admin, djangocms_link, djangocms_video, django_openid_auth, md_importer, cms, djangocms_text_ckeditor
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
... (skipped for brevity)
Applying django_comments.0003_add_submit_date_index... OK
Applying my_app.0001_initial... OK
Applying my_app.0002_example_content...Traceback (most recent call last):
...
django.db.utils.IntegrityError: Problem installing fixtures: The row in table 'django_comments' with primary key '1' has an invalid foreign key: django_comments.content
_type_id contains a value '37' that does not have a corresponding value in django_content_type.id.
loaddata
鉴于我的新迁移肯定是在其他所有操作之后运行的,因此顺序应该与我手动调用时完全相同。所以我不明白为什么它在通过 CLI 调用时会成功,但在通过迁移调用时会失败。
migrate
在我遗漏的步骤结束时是否发生了一些事情,这可能解释了这里的差异?