diff options
| author | Ben Cail <bcail@crossway.org> | 2024-09-17 15:10:39 -0400 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2024-10-31 06:37:14 +0100 |
| commit | 0eaaadd47fa00baabe12be3ed736aa016b6d327e (patch) | |
| tree | 4d05f91ac6224e7a6a658106aecfe6bee8729d65 /django/db/backends/postgresql/schema.py | |
| parent | b50d1a020d3a988ab9f45724138943dc807c5ecc (diff) | |
Fixed #35180 -- Recreated PostgreSQL _like indexes when changing between TextField and CharField field types.
Diffstat (limited to 'django/db/backends/postgresql/schema.py')
| -rw-r--r-- | django/db/backends/postgresql/schema.py | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/django/db/backends/postgresql/schema.py b/django/db/backends/postgresql/schema.py index 0c8548a5d6..75bf331472 100644 --- a/django/db/backends/postgresql/schema.py +++ b/django/db/backends/postgresql/schema.py @@ -140,6 +140,13 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): return sequence["name"] return None + def _is_changing_type_of_indexed_text_column(self, old_field, old_type, new_type): + return (old_field.db_index or old_field.unique) and ( + (old_type.startswith("varchar") and not new_type.startswith("varchar")) + or (old_type.startswith("text") and not new_type.startswith("text")) + or (old_type.startswith("citext") and not new_type.startswith("citext")) + ) + def _alter_column_type_sql( self, model, old_field, new_field, new_type, old_collation, new_collation ): @@ -147,11 +154,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): # different type. old_db_params = old_field.db_parameters(connection=self.connection) old_type = old_db_params["type"] - if (old_field.db_index or old_field.unique) and ( - (old_type.startswith("varchar") and not new_type.startswith("varchar")) - or (old_type.startswith("text") and not new_type.startswith("text")) - or (old_type.startswith("citext") and not new_type.startswith("citext")) - ): + if self._is_changing_type_of_indexed_text_column(old_field, old_type, new_type): index_name = self._create_index_name( model._meta.db_table, [old_field.column], suffix="_like" ) @@ -277,8 +280,14 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): strict, ) # Added an index? Create any PostgreSQL-specific indexes. - if (not (old_field.db_index or old_field.unique) and new_field.db_index) or ( - not old_field.unique and new_field.unique + if ( + (not (old_field.db_index or old_field.unique) and new_field.db_index) + or (not old_field.unique and new_field.unique) + or ( + self._is_changing_type_of_indexed_text_column( + old_field, old_type, new_type + ) + ) ): like_index_statement = self._create_like_index_sql(model, new_field) if like_index_statement is not None: |
