summaryrefslogtreecommitdiff
path: root/django/db/backends/postgresql/schema.py
diff options
context:
space:
mode:
authorDavid Sanders <shang.xiao.sanders@gmail.com>2022-09-24 01:40:17 +1000
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-09-25 20:23:01 +0200
commit9f8c994851804863d556854f5231316eec478bd5 (patch)
tree5dad49c673d112f0e34ed572e6e36f1ca2dc849b /django/db/backends/postgresql/schema.py
parent50096a3a7ad777fae2179bf8b20ce386b337f247 (diff)
downloaddjango-9f8c994851804863d556854f5231316eec478bd5.tar.gz
Fixed #34027 -- Fixed migrations crash when altering type of char/text fields referenced by foreign key on PostgreSQL.
Diffstat (limited to 'django/db/backends/postgresql/schema.py')
-rw-r--r--django/db/backends/postgresql/schema.py26
1 files changed, 14 insertions, 12 deletions
diff --git a/django/db/backends/postgresql/schema.py b/django/db/backends/postgresql/schema.py
index cfc30516e6..98af319570 100644
--- a/django/db/backends/postgresql/schema.py
+++ b/django/db/backends/postgresql/schema.py
@@ -131,6 +131,20 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
return ""
def _alter_column_type_sql(self, model, old_field, new_field, new_type):
+ # Drop indexes on varchar/text/citext columns that are changing to a
+ # 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"))
+ ):
+ index_name = self._create_index_name(
+ model._meta.db_table, [old_field.column], suffix="_like"
+ )
+ self.execute(self._delete_index_sql(model, index_name))
+
self.sql_alter_column_type = "ALTER COLUMN %(column)s TYPE %(type)s"
# Cast when data type changed.
if using_sql := self._using_sql(new_field, old_field):
@@ -227,18 +241,6 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
new_db_params,
strict=False,
):
- # Drop indexes on varchar/text/citext columns that are changing to a
- # different 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"))
- ):
- index_name = self._create_index_name(
- model._meta.db_table, [old_field.column], suffix="_like"
- )
- self.execute(self._delete_index_sql(model, index_name))
-
super()._alter_field(
model,
old_field,