Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.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单元测试在多个Postgres架构中失败_Python_Django_Postgresql - Fatal编程技术网

Python Django单元测试在多个Postgres架构中失败

Python Django单元测试在多个Postgres架构中失败,python,django,postgresql,Python,Django,Postgresql,My Postgres DB有3种模式:默认模式、cedirData模式和webData模式 对于那些指向不同于默认模式的模式的模型,我指定如下: class Person(models.Model): first_name = models.CharField(max_length=200, null=False, blank=False) last_name = models.CharField(max_length=200, null=False, blank=False)

My Postgres DB有3种模式:默认模式、cedirData模式和webData模式

对于那些指向不同于默认模式的模式的模型,我指定如下:

class Person(models.Model):
    first_name = models.CharField(max_length=200, null=False, blank=False)
    last_name = models.CharField(max_length=200, null=False, blank=False)

    class Meta:
        db_table = 'cedirData\".\"persons'
应用程序工作正常,但当我尝试运行测试时:

$ ./manage.py test
我得到以下信息:

  File "/home/wbrunetti/.virtualenvs/cedir/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 160, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/home/wbrunetti/.virtualenvs/cedir/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 63, in migrate
    self.apply_migration(migration, fake=fake)
  File "/home/wbrunetti/.virtualenvs/cedir/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 97, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/home/wbrunetti/.virtualenvs/cedir/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 107, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/home/wbrunetti/.virtualenvs/cedir/local/lib/python2.7/site-packages/django/db/migrations/operations/models.py", line 36, in database_forwards
    schema_editor.create_model(model)
  File "/home/wbrunetti/.virtualenvs/cedir/local/lib/python2.7/site-packages/django/db/backends/schema.py", line 270, in create_model
    self.execute(sql, params)
  File "/home/wbrunetti/.virtualenvs/cedir/local/lib/python2.7/site-packages/django/db/backends/schema.py", line 98, in execute
    cursor.execute(sql, params)
  File "/home/wbrunetti/.virtualenvs/cedir/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/wbrunetti/.virtualenvs/cedir/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/wbrunetti/.virtualenvs/cedir/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: schema "cedirData" does not exist
看起来可能与迁移有关。由于DB表已经存在,我只是创建了初始迁移并运行了一个--false:

测试数据库仅使用默认模式创建

我正在使用Django 1.7和Python 2.7.6

任何想法或想法都会有帮助


谢谢

模式没有在许多其他数据库引擎中使用。通过在模型中指定模式,您已经在postgres代码中引入了依赖项

有两种方法可以解决你的问题

首先,您可以向postgres用户添加默认搜索路径。这种方法的缺点是模式不能再用于名称空间,但优点是,如果数据库更改为其他引擎,代码将正常工作。可以通过选择一些标准的表命名方式来实现表的命名,类似于Django默认的命名方式(例如appName\u className)

有两种方法可以实现这一点。按这种方式执行的postgres命令是:

ALTER USER (your user) SET search_path = "$user",(schema1),(schema2),(schema3),(...)
django唯一的方法是:

# Warning! This is untested, I just glanced at the docs and it looks right.
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        # some configuration here
        'OPTIONS': {
            'options': '-c search_path=schema1,schema2,schema3'
        }
    }
}
您还需要更改:

db_table = 'cedirData\".\"persons'
致:

作为奖励,您现在可以使用:

manage.py inspectdb > models.py
这是一个很好的特性,这样您就不必手动复制现有的数据库

但是,如果在数据库上大量使用模式名称空间,并且其他应用程序依赖它,那么此解决方案将不会对您有所帮助。另一种方法是编写自定义testrunner,在测试数据库中创建这些模式。这比上述方法要复杂一些,可能会有点混乱。我不建议你这么做,但如果你有兴趣,我可以试着帮你

一种不那么混乱但更“黑客”的方法是在运行测试时简单地覆盖meta。这也是一个testrunner

from django.test.simple import DjangoTestSuiteRunner
from django.db.models.loading import get_models

class SchemaModelTestRunner(DjangoTestSuiteRunner):
    """Docstring"""
    def setup_test_environment(self, *args, **kwargs):
        self.original_db_tables = {}
        self.schema_models = [m for m in get_models()
                                 if '"."' in m._meta.db_table]
        for m in self.schema_models:
            schema, table = m._meta.db_table.split('"."')
            self.original_db_tables[m] = m._meta.db_table
            m._meta.db_table = 'schema_'+schema+'_table_'+table

        super(SchemaModelTestRunner, self).setup_test_environment(*args,
                                                                   **kwargs)
    def teardown_test_environment(self, *args, **kwargs):
        super(SchemaModelTestRunner, self).teardown_test_environment(*args,
                                                                      **kwargs)
        # reset models
        for m in self.schema_models:
            m._meta.db_table = self.original_db_tables[m]

您还需要将其定义为settings.py文件中的testrunner。

settings.py中的“选项”技巧是一个好主意,也是最好的选项
manage.py inspectdb > models.py
from django.test.simple import DjangoTestSuiteRunner
from django.db.models.loading import get_models

class SchemaModelTestRunner(DjangoTestSuiteRunner):
    """Docstring"""
    def setup_test_environment(self, *args, **kwargs):
        self.original_db_tables = {}
        self.schema_models = [m for m in get_models()
                                 if '"."' in m._meta.db_table]
        for m in self.schema_models:
            schema, table = m._meta.db_table.split('"."')
            self.original_db_tables[m] = m._meta.db_table
            m._meta.db_table = 'schema_'+schema+'_table_'+table

        super(SchemaModelTestRunner, self).setup_test_environment(*args,
                                                                   **kwargs)
    def teardown_test_environment(self, *args, **kwargs):
        super(SchemaModelTestRunner, self).teardown_test_environment(*args,
                                                                      **kwargs)
        # reset models
        for m in self.schema_models:
            m._meta.db_table = self.original_db_tables[m]