Opened 9 months ago

Last modified 5 days ago

#35180 closed Bug

PostgreSQL pattern ops indexes are dropped when changing between CharField and TextField — at Version 1

Reported by: Robin Ray Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: index postgres migration
Cc: Simon Charette, Mariusz Felisiak, bcail Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Robin Ray)

When converting an indexed CharField to a TextField (and vice versa) on PostgreSQL, Django drops the existing pattern ops _like index for the column but does not recreate it with the new pattern ops. When reversing the migration, Django does not recreate the initial _like index.

I have been able to reproduce this behavior in Django 3.2 and 5.0.

Reproduction

  1. Given a model with a CharField that has db_index=True on a PostgreSQL database
  2. Inspect the database indexes or migration SQL and see that the indexed CharField has two indexes, one of which ends in _like and uses varchar_pattern_ops
  3. Change the CharField to a TextField and generate a migration
  4. Run the migration or inspect the SQL with the sqlmigrate manage command
  5. Inspect the database indexes or migration SQL and see that the indexed TextField does not have a _like index
  6. Reverse the migration
  7. Inspect the database indexes or migration SQL and see that the indexed CharField no longer has a _like index

Here is an example project that demonstrates the issue: https://github.com/robin-ray/alter_text_index_repro

Change History (1)

comment:1 by Robin Ray, 9 months ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top