在迁移中调用loaddata时会引发IntegrityError,但是如果从CLI

时间:2016-05-25 21:48:28

标签: django django-migrations django-fixtures django-manage.py loaddata

对于我的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步骤,因此我尝试创建迁移{{1} 1}},调用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. 时的顺序完全相同。所以我不明白为什么它在通过CLI调用时会成功,但在通过迁移调用时会失败。

loaddata步骤结束时是否有某些事情发生了我可能会解释这里的差异?

2 个答案:

答案 0 :(得分:2)

Django使用post_migrate信号来创建缺少的内容类型,因此如果这是您第一次在当前数据库上迁移,则在迁移到加载数据之后,内容类型将不会存在。

您可以手动创建迁移所需的内容类型。 Django将检测现有的内容类型,并且不会创建任何重复项。

请注意,内容类型的特定ID可能因数据库而异。使用natural foreign keys转储数据可能更好。

答案 1 :(得分:0)

knbk's answer提示,我设法解决了这个问题。

最初我尝试手动调用update_contenttypes,但这本身并不起作用。事实证明我需要实际调用所有post_migrate信号,因为我猜他们会更多地整理所需的信号。

这是my_app.0002_example_content我最终得到的结果:

# my_app/migrations/0002_example_content.py

from django.core.management import call_command
from django.db import migrations
from django.apps import apps
from django.db.models.signals import post_migrate


def finish_previous_migrations(migrate_apps):
    """
    Explicitly run the post_migrate actions
    for all apps
    """

    for app_config in apps.get_app_configs():
        post_migrate.send(
            sender=app_config,
            app_config=app_config
        )


def load_site_content(migrate_apps, schema_editor):
    """
    Load the website content fixture
    """

    finish_previous_migrations(migrate_apps)

    call_command("loaddata", "example_content")


class Migration(migrations.Migration):
    dependencies = [
        ('djangocms_inherit', '0002_auto_20150622_1244'),
        ... all other apps (skipped for brevity)
        ('my_app', '0001_initial'),
    ]
    operations = [
        migrations.RunPython(load_site_content)
    ]