我在运行django单元测试时遇到错误,我之前没有经历过这种情况,并且整个下午都在谷歌搜索。
运行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")
运行django-admin.py sqlflush时会引用media_images表,并在运行django manage.py syncdb时生成ok。
这是图像模型,看起来很麻烦:
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
感谢您的帮助,如果我需要提供更多信息,请告诉我们。
答案 0 :(得分:2)
解决方案:确保在common.media
中明确定义子模块(例如INSTALLED_APPS
),而不仅仅是父模块(例如common
),以确保模型被拾取并且测试能够运行。
答案 1 :(得分:0)
尝试python manage.py syncdb
然后返回