由于夹具负载问题,Django单元测试失败

时间:2014-11-16 19:38:31

标签: python django

我使用灯具预先填充一些简单的辅助数据。

我可以使用manage.py loaddata fixtures/initial_data.json成功加载灯具。

当我尝试运行单元测试时出现问题。它给了我以下错误:

  File "/usr/local/lib/python3.4/dist-packages/django/db/models/query.py", line 600, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/sql/compiler.py", line 1004, in execute_sql
    cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python3.4/dist-packages/django/utils/six.py", line 549, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: Problem installing fixture '/opt/prjoect/myapp/fixtures/initial_data.json': Could not load myapp.SomeModel(pk=1): relation "myapp_somemodel" does not exist
LINE 1: UPDATE "myapp_somemodel" SET "name" = 'M' WHERE "myapp_somemodel"."id" = 1

下表列在常规数据库中。这个夹具没有外键,只有简单的表,带有键和其他值 我的迁移模块中只有一个initial_migration。

问题出在哪里?我不知道可能是什么原因。

我使用Django 1.7,python 3.4,Postrgesql,Ubuntu 14.04

1 个答案:

答案 0 :(得分:1)

您的灯具似乎正在尝试更新id = 1的对象:

LINE 1: UPDATE "myapp_somemodel" SET "name" = 'M' WHERE "myapp_somemodel"."id" = 1

并且此对象不存在:

Could not load myapp.SomeModel(pk=1)...
  • 你有myapp_somemodel1的对象id / pk = 1吗?

也许你在生成灯具时已经拥有它,并且在你删除它之后。您可以尝试在shell中执行:

from myapp.models import somemodel

new_object = Somemodel(id=1, name='anything')
new_object.save()

然后尝试再次加载夹具