gpt4 book ai didi

django - 使用SQLite使用OneToOneField加载Django固定装置时发生IntegrityError

转载 作者:行者123 更新时间:2023-12-03 19:43:00 25 4
gpt4 key购买 nike

尝试通过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之间存在OneToOne关系:

class UserProfile(TimeStampedModel):
user = models.OneToOneField(settings.AUTH_USER_MODEL, null=True, blank=True)


运行 ./manage.py dumpdata user --format=json --indent=4 --natural-foreign会产生以下结果:

CustomUser模型数据

[
{
"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_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。

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

最佳答案

我猜您应该使用Migrations https://docs.djangoproject.com/en/1.7/topics/migrations/,它们是有序的。但是,如果您使用的是Django 1.7之前的版本,请安装south https://south.readthedocs.org/en/latest/

关于django - 使用SQLite使用OneToOneField加载Django固定装置时发生IntegrityError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26487579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com