【问题标题】:ProgrammingError: (1146, "Table 'test_<DB>.<TABLE>' doesn't exist") when running unit test for DjangoProgrammingError: (1146, "Table 'test_<DB>.<TABLE>' 不存在") 为 Django 运行单元测试时
【发布时间】:2011-02-08 08:10:28
【问题描述】:

我正在使用 Django 框架运行单元测试并收到此错误。

运行实际代码没有这个问题,运行单元测试会动态创建一个测试数据库,所以我怀疑问题出在那儿。

引发错误的代码如下所示

member = Member.objects.get(email=email_address)

模型看起来像

class Member(models.Model):
    member_id = models.IntegerField(primary_key=True)
    created_on = models.DateTimeField(editable=False, default=datetime.datetime.utcnow())
    flags = models.IntegerField(default=0)
    email = models.CharField(max_length=150, blank=True)
    phone = models.CharField(max_length=150, blank=True)
    country_iso = models.CharField(max_length=6, blank=True)
    location_id = models.IntegerField(null=True, blank=True)
    facebook_uid = models.IntegerField(null=True, blank=True)
    utc_offset = models.IntegerField(null=True, blank=True)
    tokens = models.CharField(max_length=3000, blank=True)
    class Meta:
        db_table = u'member'

我看不出有什么太奇怪的地方。

运行测试的用户与运行网站的用户拥有相同的数据库服务器权限

这是在 osx 上运行的 mariadb 上的 django 1.1

    MJ-2:mysite Marsh$ python manage.py test sitecoming
Creating test database...
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table djangodblog_errorbatch
Creating table djangodblog_error
Installing index for djangodblog.ErrorBatch model
Installing index for djangodblog.Error model
E
======================================================================
ERROR: test_index (mysite.sitecoming.tests.SiteComingTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/Marsh/Development/deal/src/mysite/sitecoming/tests.py", line 19, in test_index
    response = c.post('/submit', {'email':'marshall@offby3.com'})
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/test/client.py", line 313, in post
    response = self.request(**r)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/handlers/base.py", line 92, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/Users/Marsh/Development/deal/src/mysite/sitecoming/views.py", line 49, in submit
    member = Member.objects.get(email=email_address)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/manager.py", line 120, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/query.py", line 300, in get
    num = len(clone)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/query.py", line 81, in __len__
    self._result_cache = list(self.iterator())
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/query.py", line 238, in iterator
    for row in self.query.results_iter():
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/sql/query.py", line 287, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/sql/query.py", line 2369, in execute_sql
    cursor.execute(sql, params)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 84, in execute
    return self.cursor.execute(query, args)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQL_python-1.2.3c1-py2.6-macosx-10.3-fat.egg/MySQLdb/cursors.py", line 173, in execute
    self.errorhandler(self, exc, value)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQL_python-1.2.3c1-py2.6-macosx-10.3-fat.egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
ProgrammingError: (1146, "Table 'test_deal.member' doesn't exist")

----------------------------------------------------------------------
Ran 1 test in 0.447s

FAILED (errors=1)
Destroying test database...

我还能在哪里查看问题所在,为什么没有创建此表?

更新 - 问题似乎是在运行单元测试时,用于生成测试数据库的模型来自应用程序内部而不是项目内部。这似乎是一种奇怪的行为,并且违反了 DRY,因为为了使其正常工作,我需要在每个应用程序中复制模型文件,而不是在项目中集中复制。

谁能建议如何解决这个问题?

** update 2 ** - 项目结构如下:

项目结构如下:

/mysite (www.whatever.com)
    /application1 (facebook app, handles all urls beginning with /fb)
    /application2 (www app, handles everything else in the root dir of the site)

我希望将 fb 功能与网站的其他部分分开,但它们共享相同的数据库。我做错了吗?

【问题讨论】:

  • 您似乎对模型的生存位置存在误解 - 它们确实存在于应用程序中,而不是“项目”中,后者只是具有设置文件的应用程序集合.你能发布你的项目结构吗?
  • 您确定为我们粘贴了完整的模型吗?你没有遗漏managed = True 之类的东西吗?

标签: python django unit-testing


【解决方案1】:

要纠正此问题,请生成在项目文件夹的settings.py 文件中声明的所有表。

您可以在设置文件的INSTALLED APPS 块中找到。为此运行此命令:

manage.py syncdbpython manage.py syncdb

如果这不起作用,则为 python 目录设置环境变量 PATH。

【讨论】:

【解决方案2】:

稍有不同,您绝对不希望这样:

created_on = models.DateTimeField(…, default=datetime.datetime.utcnow())

这实际上会在您的 models.py 文件加载时调用 utcnow()(当您 manage.py runserver 时会发生这种情况),并使用该单个时间点作为此后每个实例的默认值。你想要的是这样的:

created_on = models.DateTimeField(…, default=datetime.datetime.utcnow)

default 参数可以接受一个可调用对象,每次新实例需要默认值时都会调用该可调用对象。看看documentation

【讨论】:

    【解决方案3】:

    您可以通过将 -v3 添加到您的测试中来获得有关正在发生的事情的更多信息,即:

    $ python manage.py test -v3 sitecoming
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      • 2015-08-17
      • 1970-01-01
      • 2018-12-16
      • 2015-07-11
      • 2016-09-02
      • 2021-10-11
      相关资源
      最近更新 更多