Nikolai Golub
Nikolai Golub

Reputation: 3567

Django unittests fail because of fixture load issue

I use fixtures for prepopulating some simple auxilary data.

I can successfully load fixtures using manage.py loaddata fixtures/initial_data.json.

A problem appears when I try to run a unit test. It gives me the following error:

  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

Following tables are presented in regular database. This fixture doesn't have foreign keys, only simple tables, with key and other values.
I have only one initial_migration in my migrations module.

Where is the problem? I have no idea what could be the root cause.

I use Django 1.7, python 3.4, Postrgesql, Ubuntu 14.04

Upvotes: 2

Views: 1378

Answers (1)

AlvaroAV
AlvaroAV

Reputation: 10553

It seems your fixture is trying to update an object with id=1:

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

and this object does not exist:

Could not load myapp.SomeModel(pk=1)...
  • Do you have an object of myapp_somemodel1 with id/pk = 1 ?

Maybe you had it when you generate the fixture, and after you've deleted it. You could try to do in the shell:

from myapp.models import somemodel

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

And then try to load the fixture again

Upvotes: 1

Related Questions