python - Django 测试失败

标签 python mysql django unit-testing mysql-error-1146

我在运行 Django 单元测试时遇到错误,我以前没有遇到过这种情况,整个下午都在谷歌上搜索它。

运行 django manage.py 测试后,我在终端中收到此错误:

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")

media_images 表在运行 django-admin.py sqlflush 时被引用并在我运行 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

非常感谢任何帮助,如果我需要提供更多信息,请告诉我!

最佳答案

解决方案:确保在 INSTALLED_APPS 中明确定义子模块(例如 common.media),而不仅仅是父模块(例如 common)以确保拾取模型并且能够运行测试。

关于python - Django 测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2782920/

相关文章:

django - 如何使用 JDBC 驱动程序为 django 编写自定义数据库适配器?

Django 模型 : Reference field return multiple type of models

php - 将 WooCommerce 订单保存到远程数据库

php - 为什么在 PHP MySQL 中用 %40 替换 @

python - 从 Python 库的角度来看,爬行、解析、索引、搜索之间有什么区别

python - 我可以/如何通过 websocket 连接到扭曲的应用程序来访问客户端 cookie?

python - NumPy 的 : read data from CSV having numerals as string

python - 使用列表作为行中的值创建 Pandas 数据框

javascript - Python 和 JS [object HTMLSelectElement] 和 [object HTMLInputElement] 声明问题

php - 使用 PHP 从数据中提取 URL