Django测试错误:django.db.utils.ProgrammingError:(1146,“表'test_mydb.mytable'不存在”)

时间:2019-03-04 17:41:12

标签: python django django-testing django-tests django-mysql

我正在尝试使用python的TestCase类编写测试用例。当我运行python manage.test时,出现以下错误:

Operations to perform:
  Synchronize unmigrated apps: messages, rest_framework, staticfiles
  Apply all migrations: admin, auth, contenttypes, genes, sessions
Running pre-migrate handlers for application admin
Running pre-migrate handlers for application auth
Running pre-migrate handlers for application contenttypes
Running pre-migrate handlers for application sessions
Running pre-migrate handlers for application rest_framework
Running pre-migrate handlers for application files
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
Running migrations:
  Applying contenttypes.0001_initial... OK (0.031s)
  Applying auth.0001_initial... OK (0.272s)
  Applying admin.0001_initial... OK (0.082s)
  Applying admin.0002_logentry_remove_auto_add... OK (0.011s)
  Applying contenttypes.0002_remove_content_type_name... OK (0.064s)
  Applying auth.0002_alter_permission_name_max_length... OK (0.029s)
  Applying auth.0003_alter_user_email_max_length... OK (0.028s)
  Applying auth.0004_alter_user_username_opts... OK (0.012s)
  Applying auth.0005_alter_user_last_login_null... OK (0.035s)
  Applying auth.0006_require_contenttypes_0002... OK (0.006s)
  Applying auth.0007_alter_validators_add_error_messages... OK (0.011s)
  Applying auth.0008_alter_user_username_max_length... OK (0.046s)
  Applying auth.0009_alter_user_last_name_max_length... OK (0.040s)
  Applying files.0001_initial... OK (0.024s)
  Applying files.0002_auto_20190303_1656... OK (0.005s)
  Applying sessions.0001_initial... OK (0.028s)
Running post-migrate handlers for application admin
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Running post-migrate handlers for application auth
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Running post-migrate handlers for application contenttypes
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Running post-migrate handlers for application sessions
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Running post-migrate handlers for application rest_framework
Running post-migrate handlers for application files
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Adding permission 'Permission object (None)'
Traceback (most recent call last):
  File "/Python/myenv/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "//Python/myenv/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "/Python/myenv/lib/python3.7/site-packages/MySQLdb/cursors.py", line 206, in execute
    res = self._query(query)
  File "/Python/myenv/lib/python3.7/site-packages/MySQLdb/cursors.py", line 312, in _query
    db.query(q)
  File "/Python/myenv/lib/python3.7/site-packages/MySQLdb/connections.py", line 224, in query
    _mysql.connection.query(self, query)
MySQLdb._exceptions.ProgrammingError: (1146, "Table 'test_mydb.mytable’ doesn't exist")

这是我的tests.py:     类TestCase(TestCase):

@classmethod
def setUpTestData(cls):
    print("calling command")
    db_dump = “’file_data.json'"
    call_command("loaddata",db_dump,verbosity=3)


def test_statuscode(self ):
    print("Starting test cases....")
    url=‘/find_file/‘
    query_params ={'spec’:’detailed’,’name’:’ABV’}
    response = self.client.get(url,query_params)
    self.assertEqual(response.status_code,200)

这是files / models.py

class MyModel(models.Model):
spec = models.CharField(max_length=255, blank=True, null=True)
id = models.CharField(primary_key=True, max_length=128)
name = models.CharField(max_length=128, blank=True, null=True)
number = models.CharField(max_length=60, blank=True, null=True)
db = models.CharField(max_length=32)

class Meta:
    managed = True
    db_table = ‘mytable’

settings.py

from .base import *


DATABASES = {
   'default' :{
        'ENGINE' : 'django.db.backends.mysql',
        'NAME': ‘mydb_90',
        'USER': ‘xxx’,
        'PASSWORD': ‘yyy’,
        'PORT' : '3306',

   },
}

Django版本是2.0.6。 Python 3.7.1。我有两个问题: 1)为什么会出现此错误?迁移文件到位,日志显示已执行这些文件。 2)我曾期望在setupTestData方法中进行数据库和表的创建。看起来数据库和表是在调用此方法之前创建的。因此,我茫然不知如何调试它。

0 个答案:

没有答案