【问题标题】:Using Django 3+ on older Postgres (9.4)在较旧的 Postgres (9.4) 上使用 Django 3+
【发布时间】:2021-04-21 11:25:01
【问题描述】:

类似于this post,我需要在较旧的 Postgres 数据库 (v9.4) 上使用较新的 Django (v3.1) 应用程序。本文中的解决方案(升级、将数据迁移到新数据库)是更好的建议,但就短期而言,我更喜欢使用这些版本(即使 Django 3 放弃了对 9.4 的支持)。

至少有一个问题是 ON CONFLICT,Postgres v9.5 中的一个新特性,被 Django 用于多线程管理,所以一个简单的 .add() 调用包含这个 SQL。

【问题讨论】:

  • 9.4 已终止使用(7 岁),并且不再提供针对它的安全修复程序。使用生命周期结束的版本通常是非常不安全的做法
  • 这个环境恰好是孤立的,希望很快升级,但绝对是可靠的建议

标签: django postgresql-9.4


【解决方案1】:

可能会遇到其他问题的 hacky(短期)解决方案是禁用 Django 数据库后端中的 ignore_conflicts 功能。这是通过以下方式完成的:

  1. 为自定义后端创建新模块:

    # Here I'm assuming there is a 'core' module in the project;
    # a common alternative is to see the project name for this module ('<project>/db')
    cd <project>
    mkdir -p core/db
    touch core/db/__init__.py
    
  2. core/db/base.py 中的自定义后端类以禁用此功能:

    from django.db.backends.postgresql.base import DatabaseWrapper as PgDatabaseWrapper
    from django.db.backends.postgresql.features import DatabaseFeatures as PgDatabaseFeatures
    
    
    class DatabaseFeatures(PgDatabaseFeatures):
        supports_ignore_conflicts = False
    
    
    class DatabaseWrapper(PgDatabaseWrapper):
        features_class = DatabaseFeatures
    
  3. 更新设置中的数据库配置以使用新后端:

    # in settings.py
    DATABASES = {
        "default": {
            "ENGINE": "core.db",
            # ... rest of postgres settings ...
        }
    }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 2016-08-04
    • 2023-02-24
    • 2015-06-29
    • 2015-06-08
    • 1970-01-01
    相关资源
    最近更新 更多