使用SQLite加载带有OneToOneField的Django夹具时的完整性错误

使用SQLite加载带有OneToOneField的Django夹具时的完整性错误,django,sqlite,Django,Sqlite,当试图通过syncdb命令加载初始数据时,Django抛出以下错误: django.db.utils.IntegrityError: Problem installing fixtures: The row in table 'user_profile_userprofile' with primary key '1' has an invalid foreign key: user_profile_userprofile.user_id contains a value '1' tha

当试图通过
syncdb
命令加载初始数据时,Django抛出以下错误:

django.db.utils.IntegrityError: Problem installing fixtures: The row in table     'user_profile_userprofile' with primary key '1' has an invalid foreign key: user_profile_userprofile.user_id contains a value '1' that does not have a corresponding value in user_customuser.id.
UserProfile模型和CustomUser之间存在OneTONE关系:

class UserProfile(TimeStampedModel):
    user = models.OneToOneField(settings.AUTH_USER_MODEL, null=True, blank=True)
运行
/manage.py dumpdata user--format=json--indent=4--natural foreign
会产生以下结果:

客户用户模型数据

[
{
    "fields": {
        "first_name": "Test",
        "last_name": "Test",
        "is_active": true,
        "is_superuser": true,
        "is_staff": true,
        "last_login": "2014-10-21T11:33:42Z",
        "groups": [],
        "user_permissions": [],
        "password": "pbkdf2_sha256$12000$Wqd4ekGdmySy$Vzd/tIFIoSABP9J0GyDRwCgVh5+Zafn9lOiTGin9/+8=",
        "email": "test@test.com",
        "date_joined": "2014-10-21T11:22:58Z"
    },
    "model": "user.customuser",
    "pk": 1
}
]
运行
/manage.py dumpdata user\u profile--format=json--indent=4--natural foreign
会产生以下结果:

配置文件模型

[
{
    "fields": {
        "weight": 75.0,
        "created": "2014-10-21T11:23:35.536Z",
        "modified": "2014-10-21T11:23:35.560Z",
        "height": 175,
        "user": 1,
    },
    "model": "user_profile.userprofile",
    "pk": 1
}
]
只加载CustomUser模型的初始数据,然后通过加载数据跟踪UserProfile效果很好,这对我来说意味着
syncdb
正试图在加载CustomUser之前加载UserProfile


如果最简单的解决方案是强制加载顺序,那么最简单的方法是什么?

我想您应该使用
迁移,它们是有序的。但是如果您使用较旧的Django版本1.7,请安装
south

您的意思是在首次加载数据时还是在从一个模式移动到另一个模式时?它来自Django文档:自动加载自1.7版以来已弃用的初始数据装置:如果应用程序使用迁移,则不会自动加载装置。由于Django 2.0中的应用程序需要迁移,因此这种行为被认为是不推荐的。如果您想为应用程序加载初始数据,请考虑在数据迁移中进行。