Django unittest失败,未找到表

时间:2022-04-29 23:19:46
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)

I've a sample testcase like above and when I run with this command python manage.py test -v3 tests

我有一个类似于上面的示例测试用例,当我使用此命令运行python manage.py test -v3 tests时

It is throwing the bellow error.

它正在抛出波纹管错误。

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 个解决方案

#1


1  

First: Run the makemigrations.

第一:运行makemigrations。

Try this :

尝试这个 :

python manage.py makemigrations

Second: lion is a Projects object, not a queryset.
If you want get the count of Projects, try change the assert to:

第二:lion是Projects对象,而不是查询集。如果要获取项目计数,请尝试将断言更改为:

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

#1


1  

First: Run the makemigrations.

第一:运行makemigrations。

Try this :

尝试这个 :

python manage.py makemigrations

Second: lion is a Projects object, not a queryset.
If you want get the count of Projects, try change the assert to:

第二:lion是Projects对象,而不是查询集。如果要获取项目计数,请尝试将断言更改为:

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