我遇到了丢失表的情况,同时运行Django(1.3.x)测试,这些测试使用1.4版的后端版本的实时服务器正常测试运行良好但看起来实时服务器测试没有连接到正确的数据库?测试数据库定义为:
DATABASES['default']['ENGINE'] = 'django.db.backends.sqlite3'
DATABASES['default']['NAME'] = ':memory:'
DATABASES['default']['OPTIONS'] = {}
DATABASES['default']['TEST_NAME'] = ':memory:'
我在django.db.backends.creation.create_test_db
中添加了一个打印声明:
# We need to then do a flush to ensure that any data installed by
# custom SQL has been removed. The only test data should come from
# test fixtures, or autogenerated from post_syncdb triggers.
# This has the side effect of loading initial data (which was
# intentionally skipped in the syncdb).
call_command('flush',
verbosity=max(verbosity - 1, 0),
interactive=False,
database=self.connection.alias)
print 'django_site' in self.connection.introspection.table_names()
打印出来:
True
因此我们看到我们的数据库已正确设置。但是,在第一个请求中,context_processor尝试访问Site模型:
from django.contrib.sites.models import Site
Site.objects.get(pk=1)
DatabaseError: Database...o_site',)
列出所有表格,返回一个空列表:
from django.db import connection
print connection.introspection.table_names()
[]
就好像这个连接没有连接到设置期间的那个连接而是
connection.settings_dict
{'ENGINE': 'django.db.backends.sqlite3', 'TEST_MIRROR': None, 'NAME': ':memory:',
'TEST_CHARSET': None, 'TIME_ZONE': 'UTC', 'TEST_COLLATION': None, 'PORT': '',
'HOST': '', 'USER': '', 'TEST_NAME': ':memory:', 'PASSWORD': '', 'OPTIONS': {}}
看起来不错?