.' doesn' t exist") 为 Django 运行单元测试时-6ren"> .
' doesn' t exist") 为 Django 运行单元测试时-我正在使用 Django 框架运行单元测试并收到此错误。 运行实际代码没有这个问题,运行单元测试会即时创建一个测试数据库,所以我怀疑问题出在那里。 抛出错误的代码如下所示 member = Membe-6ren">
gpt4 book ai didi

python - ProgrammingError : (1146, "Table ' test_.

' doesn' t exist") 为 Django 运行单元测试时
转载 作者:太空狗 更新时间:2023-10-29 22:12:01 25 4
gpt4 key购买 nike

我正在使用 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,因为为了让它工作,我需要在每个应用程序中复制模型文件,而不是在项目中集中复制。

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

** 更新 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 功能与站点的其余部分分开,但它们共享同一个数据库。我做错了吗?

最佳答案

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

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

manage.py syncdbpython manage.py syncdb

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

关于python - ProgrammingError : (1146, "Table ' test_<DB>.<TABLE >' doesn' t exist") 为 Django 运行单元测试时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2644749/

25 4 0
文章推荐: python - 如何在 Python 中评估自定义数学表达式
文章推荐: c# - 在 ASP.NET C# SQL 中导致问题的文本框
文章推荐: c# - 每个请求的 log4net.GlobalContext.Properties 是否不同?
文章推荐: python - python、postgresql中的数据库测试
太空狗
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
全站热门文章
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com