Django unittest失败,找不到表

时间:2015-10-12 02:51:15

标签: python mysql django python-2.7 django-models

from django.test import TestCase
from data.models import Projects

class ProjectTestCase(TestCase):

    def test_project(self):

        Projects.objects.create(name="lion")
        lion = Projects.objects.get(name="lion")
        self.assertEqual(lion.count(), 1)

我使用上面的示例测试用例以及使用此命令运行时 python manage.py test -v3 tests

它正在抛出波纹管错误。

Traceback (most recent call last):
  File "/Users/yekabathula/workspace/roster/tests/test_project.py", line 12, in test_animals_can_speak
    Projects.objects.create(name="lion")
  File "/Library/Python/2.7/site-packages/django/db/models/manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 348, in create
    obj.save(force_insert=True, using=self.db)
  File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 710, in save
    force_update=force_update, update_fields=update_fields)
  File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 738, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 822, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 861, in _do_insert
    using=using, raw=raw)
  File "/Library/Python/2.7/site-packages/django/db/models/manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 920, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py", line 963, in execute_sql
    cursor.execute(sql, params)
  File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Library/Python/2.7/site-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 124, in execute
    return self.cursor.execute(query, args)
  File "/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.10-intel.egg/MySQLdb/cursors.py", line 202, in execute
    self.errorhandler(self, exc, value)
  File "/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.10-intel.egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
ProgrammingError: (1146, "Table 'test_nst2015.projects' doesn't exist")

----------------------------------------------------------------------
Ran 1 test in 0.031s

FAILED (errors=1)
Destroying test database for alias 'default' ('test_nst2015')...

1 个答案:

答案 0 :(得分:1)

第一: 运行makemigrations

试试这个:

python manage.py makemigrations

第二:lionProjects object,而不是查询集 如果您想获得Projects的计数,请尝试将断言更改为:

self.assertEqual(Projects.objects.count(), 1)