我試圖創建一些裝置來在Django中運行測試。現在,我只是從我的開發數據庫轉儲適當的模型,然後通過測試加載這些模型。下面是我用轉儲燈具的命令:當在Django裝置中加載auth.user和配置文件模型時出現IntegrityError
python manage.py dumpdata accounts.Profile auth.User -n auth.User --indent 4 -e contenttypes > path/to/fixture.json
繼this question和this one使用自然鍵和排除的內容類型我已經添加標誌。這並沒有幫助 - 我收到此錯誤信息:
IntegrityError: Could not load accounts.Profile(pk=1): duplicate key value violates unique constraint "accounts_profile_user_id_key"
DETAIL: Key (user_id)=(1) already exists
我已經手動檢查夾具和只有一個針對該用戶ID條目。個人資料模型非常標準,還有一些額外的個人信息字段。它連接到用戶的模型如下:
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
post_save.connect(create_user_profile, sender=User)
在完整的名字,這裏的燈具的樣子:
{
"pk": 1,
"model": "auth.user",
"fields": {
"username": "unique_username",
"first_name": "",
"last_name": "",
"is_active": true,
"is_superuser": true,
"is_staff": true,
"last_login": "2013-03-31T23:19:44.391",
"groups": [],
"user_permissions": [],
"password": "secret",
"email": "email",
"date_joined": "2013-03-13T21:30:39.225"
}
},
{
"pk": 1,
"model": "accounts.profile",
"fields": {
"status": "active",
"first_name": "John",
"last_name": "Smith",
"middle_name": null,
"headline": "Something very cool",
"user": [
"unique_username"
],
"location": null
}
}
任何想法?是因爲我使用鏈接用戶和個人資料的鉤子嗎?
我跑的Django 1.4使用Python 2.6的Enthought發行版,在Mac OS X(10.7.5)。
是否Django的測試數據庫已經包含了所有的數據?我想,可能是錯誤的,測試數據庫中的唯一數據是我包含在夾具中的數據。 – tchaymore