Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用多个数据库进行Django 1.8测试时出错_Python_Django_Database Mirroring_Multi Database - Fatal编程技术网

Python 使用多个数据库进行Django 1.8测试时出错

Python 使用多个数据库进行Django 1.8测试时出错,python,django,database-mirroring,multi-database,Python,Django,Database Mirroring,Multi Database,我正在将Django 1.8项目从单一数据库设置转移到编写器/读取器设置。我遇到了中描述的问题,但所描述的解决方法似乎没有帮助 有人遇到过类似的问题吗?相关代码段如下: 路由器: class DatabaseRouter(object): """Router to handle separation of reads and writes.""" def db_for_read(self, model, **hints): """Reads go to a r

我正在将Django 1.8项目从单一数据库设置转移到编写器/读取器设置。我遇到了中描述的问题,但所描述的解决方法似乎没有帮助

有人遇到过类似的问题吗?相关代码段如下:

路由器:

class DatabaseRouter(object):

    """Router to handle separation of reads and writes."""

    def db_for_read(self, model, **hints):
        """Reads go to a read replica."""
        return 'read'

    def db_for_write(self, model, **hints):
        """Writes always go to default."""
        return 'default'

    def allow_relation(self, obj1, obj2, **hints):
        """Allow relations bet/n objects in the same DB cluster."""
        db_list = ('default', 'read')
        if obj1._state.db in db_list and obj2._state.db in db_list:
            return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """All models end up in this pool."""
        return True
相关数据库设置

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': DB_NAME,
        'USER': DB_USER,
        'PASSWORD': DB_PASS,
        'HOST': DB_WRITE
    },
    'read': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': DB_NAME,
        'USER': DB_USER,
        'PASSWORD': DB_PASS,
        'HOST': DB_READ,
        'TEST': {
            'MIRROR': 'default',
        },
    }
}

DATABASE_ROUTERS = ['my_project.routers.DatabaseRouter']
解决复制测试用例:

class ReplicationTestCase(TestCase):

    @classmethod
    def setUpClass(cls):
        super(ReplicationTestCase, cls).setUpClass()
        connections['read']._orig_cursor = connections['read'].cursor
        connections['read'].cursor = connections['default'].cursor

    @classmethod
    def tearDownClass(cls):
        connections['read'].cursor = connections['read']._orig_cursor
        super(ReplicationTestCase, cls).tearDownClass()

有什么突然冒出来的吗?我很乐意从我们的测试环境中提供stacktraces,如果这也有用的话。谢谢

这个实际的错误似乎已经修复,但我最近遇到了一个相关的错误,解决方法可能适用于这种情况:我只是使用了一个新的设置文件,扩展并重写了数据库配置,以移动到一个单一的数据库结构。然后可以调用测试函数,如:python manage.py test--settings=app\u name.settings\u test