2010-05-06 216 views
6

我在運行django單元測試時遇到了一個錯誤,我以前沒有經歷過這種情況,並且一直在使用Google搜索整個下午。Django測試失敗

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:

我正在Django的manage.py測試後獲得在終端這個錯誤。

這是似乎是麻煩的圖像模型:

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

因此,圖像模型住在你的'媒體'應用程序,是嗎?它確實在您安裝的應用程序中,以便它由testrunner同步? – 2010-05-06 17:14:27

+1

媒體應用程序在名爲'common'的應用程序/項目中,我已將其添加到我的installed_apps。我在列表中添加了'common.media'並且測試正常,謝謝:-) 如果其他人有類似的問題,值得注意的是syncdb和sqlall拿起媒體應用沒有問題,只有管理員.py測試失敗。 – 2010-05-06 18:47:05

+0

很酷。很高興它被排序 – 2010-05-06 22:18:16

回答

2

解決方案:確保你明確地定義子模塊(例如common.media)在INSTALLED_APPS而不僅僅是父模塊(例如common),以確保該模型拿起和測試能夠運行。

0

嘗試python manage.py syncdb然後回去