KVISH
KVISH

Reputation: 13178

Django tests failing with create table

I'm not able to run my tests in Django anymore. When I try to run them as per ./manage.py test core, I get the following error:

  ...
  File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 96, in __iter__
    self._fetch_all()
  File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 857, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 220, in iterator
    for row in compiler.results_iter():
  File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 713, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 451, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: core_management

It keeps failing at that line, however, it normally creates these tables when running the tests. Is there anything i'm doing wrong? I'm not able to tell why my tests have started to fail only recently.

EDIT

I'm noticing that it's failing with a message on an M2M field:

management_map = models.ManyToManyField(Management, default=[t.pk for t in Management.objects.all()]

I have a model Object and it's failing because of the Management.objects.all(). And then it's not able to proceed because the table doesn't exist. Is there anything i'm doing wrong? The code works perfectly when run normally.

Upvotes: 0

Views: 160

Answers (1)

Serafeim
Serafeim

Reputation: 15084

The problem (if my psychic debugging is correct) is that the code that defines the management_map field is executed before the Management table has been created. So the Management.objects.all() method throws an exception.

This does not pose a problem in your normal database since both tables exist -- however I believe that if you deleted the database and tried to do syncdb again you'll experience the same error.

In any case, a Q+D way to fix it is to make sure that the Management table has been created when the management_map field is declared. Try to put the Management class definition at the top of your source file of they are in a different file then try reordering your imports.

Upvotes: 1

Related Questions