10

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?

2
  • You seem to have a misunderstanding about where models live - they do live in the application, not the "project", which is just a collection of apps with a settings file. Can you post your project structure? Commented Apr 15, 2010 at 12:32
  • Have you definitely pasted the full model for us? You haven't left out something like managed = True have you? Commented Apr 11, 2011 at 22:35

3 Answers 3

11

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.

Sign up to request clarification or add additional context in comments.

3 Comments

How table are defined in settings file?
settings.py in INSTALLED_APPS? really?
"syncdb" is deprecated from Django 1.8. Please refer this post stackoverflow.com/questions/27583744/django-table-doesnt-exist/…
4

On a slightly different note, you definitely do not want this:

created_on = models.DateTimeField(…, default=datetime.datetime.utcnow()) 

This actually calls utcnow() when your models.py file is loaded (which will happen when you manage.py runserver) and use that single point in time for the default value for every instance there-after. What you want is this:

created_on = models.DateTimeField(…, default=datetime.datetime.utcnow) 

The default argument can accept a callable, which will be called each time the default value is needed for a new instance. Have a look at the documentation.

Comments

3

You may get more information on what is going on by adding -v3 to your test, ie:

$ python manage.py test -v3 sitecoming 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.