Dolan Antenucci
Dolan Antenucci

Reputation: 15932

Using Django 3+ on older Postgres (9.4)

Similar to this post, I need to use a newer Django (v3.1) application on an older Postgres database (v9.4). The solutions in this article (upgrading, migrating data to new database) are the better advice, but for the short-term, I would prefer this work with these versions (even though Django 3 dropped support for 9.4).

One issue at least is that ON CONFLICT, a newer feature in Postgres v9.5, is used by Django for manytomany management, so a simple .add() call includes this SQL.

Upvotes: 2

Views: 899

Answers (1)

Dolan Antenucci
Dolan Antenucci

Reputation: 15932

The hacky (short-term) solution, which may run into other issues down the line, was to disable the ignore_conflicts feature in the Django database backend. This was accomplished as follows:

  1. Create new module for custom backend:

    # 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. Custom backend classes in core/db/base.py to disable this feature:

    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. Update database config in settings to use new backend:

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

Upvotes: 1

Related Questions