2013-04-02 22 views
2

我试图创建一些装置来在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 questionthis 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)。

回答

0

的问题是,你正在试图灯具加载到已经包含这些行的数据库,所以它抛出一个IntegrityError。您可以在运行loaddata之前删除表,但实际上只能使用灯具来初始填充表。

+0

是否Django的测试数据库已经包含了所有的数据?我想,可能是错误的,测试数据库中的唯一数据是我包含在夹具中的数据。 – tchaymore

1

我遇到了同样的问题。在我的情况下,我确认用户id = 1的数据库行不存在(通过User.objects.all())。

在我的情况下,原来,我是装两个固定器:

  • initial_preferences.json(自定义用户偏好)
  • initial_admin_user.json

偏好固定模式有一个ForeignKey引用用户。如果我首先加载首选项,即使仍然没有User id = 1,我也会得到重复的密钥错误。

交换顺序加载admin_user偏好固定问题的萌前。

所以 - 检查,看看是否你已经创建了具有用户ID = 1的参考其它模型对象。

0

这可能不是一个offtopic,但由于谷歌点位置o与装载固定装置与例如类似的问题。 contenttypes.ContentType即时帮助。

使用natural keys可以避免在数据更改主键时重复键值冲突。

dumpdata中添加--natural-foreign选项,例如:

$ python manage.py dumpdata --natural-foreign contenttypes auth.Permission auth.Group 
1

这往往与OneToOneFields(),如果您有发生:

Profile.user = models.OneToOneField(用户)当Django创建用户记录它会自动为这些用户创建配置文件(可能通过post_save)。因此,当loaddata开始导入配置文件时,每个用户都有一个配置文件,并且其他配置文件会打破该限制。

我有2个型号通过OneToOneField指向用户:

  • 型材;
  • 日历。

要解决该问题的方法:

1)出口auth.user到一个单独的auth_user.json;

./manage.py dumpdata --indent=4 auth.user > auth_user.json 

2)出口其他型号:

./manage.py dumpdata --indent=4 -e sessions -e admin -e contenttypes -e auth.Permission --natural-foreign > other_models.json 

3)负荷用户记录: ./manage.py loaddata AUTH_USER

4)开放./manage.py shellshell_plus 并删除所有配置文件和日历记录:

Profiles.objects.all().delete() 
Profiles.objects.all().count() 
Calendar.objects.all().delete() 
Calendar.objects.all().count() 

5)加载的记录的其余部分:

./manage.py loaddata other_models 
相关问题