我正在运行python manage.py loaddata 'path/to/mydata.json'
一个空数据库(User
并且UserProfile
创建了表但未填充表),但是,我收到以下错误:
django.db.utils.IntegrityError: Problem installing fixture 'path/to/mydata.json': Could not load myapp.UserProfile(pk=1): UNIQUE constraint failed: myapp_userprofile.user_id
我检查了(即使在运行此命令之后)并且根本没有填充数据库。那么如何给出 pk 不是唯一的错误呢?
如果相关,UserProfile
只需使用关系扩展默认User
模型,如此处OneToOneField
建议。
以下是mydata.json
包含的内容:
[
{
"model": "auth.user",
"pk": 1,
"fields": {
"password": "pbkdf2_sha256..",
"last_login": "2016-10-22T15:19:46.926Z",
"is_superuser": true,
"username": "thesuperuser",
"first_name": "",
"last_name": "",
"email": "a@a.co",
"is_staff": true,
"is_active": true,
"date_joined": "2016-10-22T14:48:27.394Z",
"groups": [],
"user_permissions": []
}
},
{
"model": "auth.user",
"pk": 2,
"fields": {
"password": "pbkdf2_sha256..",
"last_login": null,
"is_superuser": false,
"username": "user1",
"first_name": "User",
"last_name": "One",
"email": "",
"is_staff": false,
"is_active": true,
"date_joined": "2016-10-22T15:20:32Z",
"groups": [],
"user_permissions": []
}
},
{
"model": "auth.user",
"pk": 4,
"fields": {
"password": "pbkdf2_sha256..",
"last_login": null,
"is_superuser": false,
"username": "user3",
"first_name": "User",
"last_name": "Three",
"email": "",
"is_staff": false,
"is_active": true,
"date_joined": "2016-10-22T15:21:09Z",
"groups": [],
"user_permissions": []
}
},
{
"model": "auth.user",
"pk": 3,
"fields": {
"password": "pbkdf2_sha256..",
"last_login": null,
"is_superuser": false,
"username": "user2",
"first_name": "User",
"last_name": "Two",
"email": "",
"is_staff": false,
"is_active": true,
"date_joined": "2016-10-22T15:21:03Z",
"groups": [],
"user_permissions": []
}
},
{
"model": "myapp.userprofile",
"pk": 1,
"fields": {
"user": 1,
"money": 100
}
},
{
"model": "myapp.userprofile",
"pk": 2,
"fields": {
"user": 2,
"money": 100
}
},
{
"model": "myapp.userprofile",
"pk": 3,
"fields": {
"user": 3,
"money": 100
}
},
{
"model": "myapp.userprofile",
"pk": 4,
"fields": {
"user": 4,
"money": 100
}
}
]
谢谢你的帮助,