【发布时间】:2011-02-16 11:55:00
【问题描述】:
我在运行 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。
这是看起来很麻烦的Image模型:
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
感谢您的帮助,如果我需要提供更多信息,请告诉我!
【问题讨论】:
-
所以 Image 模型存在于您的“媒体”应用程序中,是吗?它肯定在您安装的应用程序中,以便测试运行程序同步它吗?
-
媒体应用程序位于一个名为“common”的应用程序/项目中,我已将其添加到我的已安装应用程序中。我将“common.media”添加到列表中并且测试有效,谢谢 :-) 如果其他人有类似的问题,值得注意的是,syncdb 和 sqlall 选择了媒体应用程序没有问题,只有 manage.py测试失败。
-
请提交答案并将其标记为已接受,以保持 StackOverflow 干净。
-
嗯,不确定“保持如此干净”,但我同意@rennat。你能以对他人有所帮助的方式回答你自己的问题吗?如果你这样做,你可以选择你的作为正确答案。这可能看起来很奇怪,但它是处理此类情况的首选方式。
标签: python mysql django unit-testing mysql-error-1146