Skip to content

[bug] Missing PostgreSQL text -> jsonb migration for JSONField replacement #798

Description

@pandafy

Describe the bug

The migration replacing jsonfield.fields.JSONField with Django’s built-in models.JSONField does not correctly migrate PostgreSQL columns from text to jsonb.

As a result, upgraded installations may still have columns typed as text while Django ORM starts generating jsonb queries/operators for these fields, causing errors such as::

operator does not exist: text = jsonb

The issue was introduced in #742

Steps To Reproduce

Note

This issue can only be reproduced on PostgreSQL.

  1. Install version 1.2 of openwisp-monitoring.

  2. Run migrations::

    python manage.py migrate
    
  3. Upgrade to the current master branch containing the jsonfield.fields.JSONField -> models.JSONField replacement.

  4. Inspect the migration SQL::

    python manage.py sqlmigrate <app_name> <migration_number>
    
  5. Observe that Django generates::

    -- (no-op)
    

    instead of::

    ALTER TABLE <table_name>
    ALTER COLUMN <column_name>
    TYPE jsonb
    USING <column_name>::jsonb;
    

Expected behavior

The migration should generate PostgreSQL ALTER COLUMN ... TYPE jsonb statements so existing text columns are correctly converted to jsonb.

Possible solution

Do not replace historical jsonfield.fields.JSONField usages in old migration files with models.JSONField.

Since the original third-party JSONField inherited from models.TextField, preserving the historical field as TextField allows Django to correctly detect the transition from text to jsonb and generate the required SQL migration.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No fields configured for Bug.

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions