我正在使用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
在设置文件的块中找到.为此运行此命令:
manage.py syncdb
要么 python manage.py syncdb
如果这不起作用,则为python目录设置环境变量PATH.