在django测试期间加载夹具时的IntegrityError

时间:2011-06-22 22:16:22

标签: django django-testing django-fixtures

我正在加载使用dumpdata创建的夹具,并获得以下异常:

Problem installing fixture 'db_dump.json': Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/django/core/management/commands/loaddata.py", line 174, in handle
    obj.save(using=using)
  File "/usr/lib/python2.6/site-packages/django/core/serializers/base.py", line 165, in save
    models.Model.save_base(self.object, using=using, raw=True)
  File "/usr/lib/python2.6/site-packages/django/db/models/base.py", line 526, in save_base
    rows = manager.using(using).filter(pk=pk_val)._update(values)
  File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 491, in _update
    return query.get_compiler(self.db).execute_sql(None)
  File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 869, in execute_sql
    cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
  File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 735, in execute_sql
    cursor.execute(sql, params)
  File "/usr/lib/python2.6/site-packages/django/db/backends/sqlite3/base.py", line 234, in execute
    return Database.Cursor.execute(self, query, params)
IntegrityError: columns app_label, model are not unique

这是一个sqlite3后端。

更新:使用自然键在这里没有什么区别。

这是什么意思,为什么会发生?

2 个答案:

答案 0 :(得分:47)

显然,一个粗心的陷阱是,在导出灯具时必须排除内容类型。 (感谢在#django上包含信息)。

要排除内容类型,请在运行dumpdata命令时使用-e选项。

$./manage.py dumpdata -e contenttypes > initial_data.json

答案 1 :(得分:9)

python manage.py dumpdata --exclude=contenttypes --exclude=auth.Permission > initial_data.json