6

I am experiencing an error running django unit tests, I've not experienced this before, and have been googling it all afternoon.

I am getting this error in terminal after running django manage.py test:

Error: Database test_unconvention couldn't be flushed. Possible reasons:
  * The database isn't running or isn't configured correctly.
  * At least one of the expected database tables doesn't exist.
  * The SQL was invalid.
Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run.
The full error: (1146, "Table 'test_unconvention.media_image' doesn't exist")

The media_images table is referenced when running django-admin.py sqlflush and generates ok when I run django manage.py syncdb.

This is the Image model which appears to be troublesome:

from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic

class Image(models.Model):
  local_image = models.ImageField(upload_to="uploads/%Y/%m/%d/", height_field="height", width_field="width", max_length=255, null=True, blank=True)
  remote_image = models.CharField(editable=False, max_length=255, null=True, blank=True)
  thirdparty_page = models.CharField(editable=False, max_length=255, blank=True, null=True)
  size = models.CharField(editable=False, max_length=25, blank=True, null=True)
  content_type = models.ForeignKey(ContentType)
  object_id = models.PositiveIntegerField()
  content_object = generic.GenericForeignKey('content_type', 'object_id')
  height = models.PositiveIntegerField(editable=False, blank=True, null=True)
  width = models.PositiveIntegerField(editable=False, blank=True, null=True)
  created_at = models.DateTimeField(editable=False, auto_now_add=True)
  updated_at = models.DateTimeField(editable=False, auto_now=True)

  def __unicode__(self):
    if self.local_image:
      return self.local_image.name
    else:
      return self.remote_image

I appreciate any help, please let me know if I should provide more information!

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Martin Ogden
  • 872
  • 4
  • 14
  • So that Image model lives in your 'media' app, yep? Is it definitely in your installed apps so that it's being synced by the testrunner? – Steve Jalim May 06 '10 at 17:14
  • 1
    The media app is inside an app/project called 'common', which i had added to my installed_apps. I added 'common.media' to the list and the tests worked, thank you :-) If anyone else has a similar problem, It's worth noting that the syncdb and sqlall picked up the media app no problem, and only the manage.py test failed. – Martin Ogden May 06 '10 at 18:47
  • 11
    please submit an answer and mark it as accepted to keep StackOverflow clean. – rennat Feb 25 '11 at 19:02
  • 2
    Um, not sure about "keeping SO clean", but I agree with @rennat. Can you answer your own question in a way that would help others? If you do, you can select yours as the correct answer. It may seem strange, but it is the preferred way of dealing with situations like this. –  Jun 13 '11 at 15:10

2 Answers2

2

Solution: Make sure you explicitly define submodules (e.g. common.media) in INSTALLED_APPS and not just the parent module (e.g. common) to make sure that the models are picked up and the test is able to run.

Martin Ogden
  • 872
  • 4
  • 14
0

try the python manage.py syncdb then go back

hustljian
  • 965
  • 12
  • 9