django - 在 Django 测试期间加载夹具时出现 IntegrityError

标签 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 后端。

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

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

最佳答案

显然,粗心大意的陷阱之一是在导出设备时必须排除内容类型。 (感谢在#django 上提供信息)。

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

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

关于django - 在 Django 测试期间加载夹具时出现 IntegrityError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6447214/

相关文章:

带时间戳的 Django 过滤器模型

Django EmbeddedModelField 在执行 PUT 请求时说 "This field may not be blank",尽管字段具有 "blank=True"

django - 创建用于域重定向的 htaccess 文件(部署在 Heroku 中的 Django 应用程序)

Django 模块化测试

python - 使用 assertRaises 的 Django 测试未能意识到引发的异常是预期的异常

python - Django 查询集 : Check if parent model has a child/referring model

python - Django 数据迁移在运行 manage.py test 时失败,但在运行 manage.py migrate 时失败

python - 未使用 Django Testserver 固定装置

django-taggit - 如何在django taggit标签模型中设置初始标签数据