Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProgrammingError: (1146, "Table 'test_<DB>.<TABLE>' doesn't exist") when running unit test for Django

I'm running a unit test using the Django framework and get this error.

Running the actual code does not have this problem, running the unit tests creates a test database on the fly so I suspect the issue lies there.

The code that throws the error looks like this

member = Member.objects.get(email=email_address)

and the model looks like

class Member(models.Model):
    member_id = models.IntegerField(primary_key=True)
    created_on = models.DateTimeField(editable=False, default=datetime.datetime.utcnow())
    flags = models.IntegerField(default=0)
    email = models.CharField(max_length=150, blank=True)
    phone = models.CharField(max_length=150, blank=True)
    country_iso = models.CharField(max_length=6, blank=True)
    location_id = models.IntegerField(null=True, blank=True)
    facebook_uid = models.IntegerField(null=True, blank=True)
    utc_offset = models.IntegerField(null=True, blank=True)
    tokens = models.CharField(max_length=3000, blank=True)
    class Meta:
        db_table = u'member'

there's nothing too odd there i can see.

the user running the tests has the same permissions to the database server as the user that runs the website

this is django 1.1 on mariadb running on osx

    MJ-2:mysite Marsh$ python manage.py test sitecoming
Creating test database...
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table djangodblog_errorbatch
Creating table djangodblog_error
Installing index for djangodblog.ErrorBatch model
Installing index for djangodblog.Error model
E
======================================================================
ERROR: test_index (mysite.sitecoming.tests.SiteComingTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/Marsh/Development/deal/src/mysite/sitecoming/tests.py", line 19, in test_index
    response = c.post('/submit', {'email':'[email protected]'})
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/test/client.py", line 313, in post
    response = self.request(**r)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/handlers/base.py", line 92, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/Users/Marsh/Development/deal/src/mysite/sitecoming/views.py", line 49, in submit
    member = Member.objects.get(email=email_address)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/manager.py", line 120, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/query.py", line 300, in get
    num = len(clone)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/query.py", line 81, in __len__
    self._result_cache = list(self.iterator())
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/query.py", line 238, in iterator
    for row in self.query.results_iter():
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/sql/query.py", line 287, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/sql/query.py", line 2369, in execute_sql
    cursor.execute(sql, params)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 84, in execute
    return self.cursor.execute(query, args)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQL_python-1.2.3c1-py2.6-macosx-10.3-fat.egg/MySQLdb/cursors.py", line 173, in execute
    self.errorhandler(self, exc, value)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQL_python-1.2.3c1-py2.6-macosx-10.3-fat.egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
ProgrammingError: (1146, "Table 'test_deal.member' doesn't exist")

----------------------------------------------------------------------
Ran 1 test in 0.447s

FAILED (errors=1)
Destroying test database...

where else can I look to see what's going wrong, why is this table not being created?

update - the issue seems to be that when running the unit tests the models that are used to generate the test database come from inside the application instead of from inside the project. this seems like odd behaviour and a violation of DRY in that in order to make it work i need to duplicate the model file in each application instead of centrally in the project.

can anyone suggest how to work around this?

** update 2 ** - project structure looks like this:

project structure looks like:

/mysite (www.whatever.com)
    /application1 (facebook app, handles all urls beginning with /fb)
    /application2 (www app, handles everything else in the root dir of the site)

i want to keep fb functionality separate from the rest of the site but they share the same db. am i doing it wrong?

like image 879
mjallday Avatar asked Apr 15 '10 11:04

mjallday


1 Answers

To rectify this problem generate all your table which were declared in the settings.py file in your project folder.

You can find the in the INSTALLED APPS Block in the settings file. For that run this command:

manage.py syncdb or python manage.py syncdb

If this doesn't work then set the Environment variable PATH for the python directory.

like image 127
ɹɐqʞɐ zoɹǝɟ Avatar answered Sep 29 '22 17:09

ɹɐqʞɐ zoɹǝɟ