gpt4 book ai didi

Django、Postgis 和 Heroku : relation "spatial_ref_sys" does not exist

转载 作者:行者123 更新时间:2023-11-29 11:51:42 25 4
gpt4 key购买 nike

我在我的应用程序上使用 django-tenant-schemas 进行 Multi-Tenancy (参见:https://github.com/bernardopires/django-tenant-schemas)

它在本地运行良好,但在 Heroku 上,当我尝试为非公共(public)模式运行迁移时遇到问题:

我的后端引擎是django.contrib.gis.db.backends.postgis;当 django 创建一个非公共(public)模式并将搜索路径设置为新创建的迁移模式时,它会尝试运行 CREATE EXTENSION IF NOT EXISTS postgis,这会引发以下错误:

NOTICE:  extension "postgis" already exists, skipping
ERROR: relation "spatial_ref_sys" does not exist

如果我在我的公共(public)模式上运行相同的命令,psql 会优雅地处理它并给我通知,当然它只发生在 heroku 上。

不确定它是否相关,但我的构建包中包含以下内容: https://github.com/cyberdelia/heroku-geo-buildpack

我搜索了很多,但到目前为止还没有找到解决方案。非常感谢任何有关如何修复此错误的帮助! :)

最佳答案

我设法解决了这个问题,请参阅:PosGis and Django-Tenants

这里的解决方法:

问题似乎是由默认的 PostGis 后端引起的,特别是调用准备迁移数据库的调用,方法是在调用 CREATE EXTENSION IF NOT EXISTS postgis 之前明确设置搜索路径 我能够通过创建覆盖此行为的自定义数据库后端来迁移/创建模式:

from django.contrib.gis.db.backends.postgis.base import (
DatabaseWrapper as OriginalPostGisDatabaseWrapper,
)
from django_tenants.utils import get_public_schema_name


class DatabaseWrapper(OriginalPostGisDatabaseWrapper):
"""
This database wrapper explicitly sets the search path when preparing the database, as
multi-schema environments (like with Django-tenants) can cause issues with the PostGis
backend.
"""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.PUBLIC_SCHEMA_NAME = get_public_schema_name()

def prepare_database(self):
# Check that postgis extension is installed.
with self.cursor() as cursor:
cursor.execute('SET search_path = %s', params=[self.PUBLIC_SCHEMA_NAME])
cursor.execute("CREATE EXTENSION IF NOT EXISTS postgis")

然后,将您的 ORIGINAL_BACKEND 设置设置为上述数据库后端的位置,而不是标准的 PostGis 后端。

关于Django、Postgis 和 Heroku : relation "spatial_ref_sys" does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35829795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com