1

我在运行 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': {}}

看起来对吗?

4

0 回答 0