多个Postgres模式的Django单元测试失败

2024-09-28 19:05:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我的Postgres数据库有3个模式:default、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'

应用程序运行正常,但当我尝试运行测试时:

^{pr2}$

我得到以下信息:

  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表已经存在,所以我创建了初始迁移并运行了一个--fake:

$ python manage.py makemigrations
$ ./manage.py migrate --fake

测试数据库仅使用默认架构创建。在

我使用的是django1.7和python2.7.6。在

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

谢谢!在


Tags: djangoinpyhomeexecutedblibpackages
1条回答
网友
1楼 · 发布于 2024-09-28 19:05:59

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

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

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

有两种方法可以做到这一点。这样做的postgres命令是:

ALTER USER (your user) SET search_path = "$user",(schema1),(schema2),(schema3),(...)

django唯一的办法是:

^{pr2}$

您还需要更改:

db_table = 'cedirData\".\"persons'

收件人:

db_table = 'persons'

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

manage.py inspectdb > models.py

这是一个很好的特性,这样您就不必手动复制现有的数据库。在

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

一种不那么混乱,但更“老套”的方法是在运行测试时简单地重写meta。这也是一个测试程序。在

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]

您还需要将其定义为设置.py文件。在

相关问题 更多 >