【问题标题】:Install hstore extension for django tests为 django 测试安装 hstore 扩展
【发布时间】:2018-08-16 12:43:00
【问题描述】:

我正在尝试在运行 django 测试之前安装 hstore 扩展。为此,我重写了默认的 DiscoverRunnersetup_databases 方法。

但是,未安装扩展程序,测试显示此错误 django.db.utils.ProgrammingError: type "hstore" does not exist

这是我覆盖默认发现运行器的代码。

settings.py

TEST_RUNNER = 'project.tests.CustomDiscovererRunner'

tests.py

from django.db import DEFAULT_DB_ALIAS, connections
from django.test.runner import DiscoverRunner

class CustomDiscovererRunner(DiscoverRunner):
    def setup_databases(self, **kwargs):

        result = super().setup_databases(**kwargs)

        connection = connections[DEFAULT_DB_ALIAS]
        cursor = connection.cursor()
        cursor.execute('CREATE EXTENSION IF NOT EXISTS HSTORE')

        return result

【问题讨论】:

标签: python django django-rest-framework django-testing django-tests


【解决方案1】:

迟到了,但是……对于问同样问题的新人:

Django 有自己的函数来使用迁移(在测试之前执行)创建扩展,示例是 HStore

https://docs.djangoproject.com/en/dev/ref/contrib/postgres/operations/#creating-extension-using-migrations

不过……

对于大多数扩展,这需要具有超级用户权限的数据库用户。如果 Django 数据库用户没有适当的权限,您必须在 Django 迁移之外使用拥有这些权限的用户创建扩展。在这种情况下,连接到您的 Django 数据库并运行查询 CREATE EXTENSION IF NOT EXISTS hstore;。

【讨论】:

    猜你喜欢
    • 2013-04-27
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    相关资源
    最近更新 更多