summaryrefslogtreecommitdiff
path: root/django/db/backends/postgresql/schema.py
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2022-12-22 07:12:17 +0100
committerGitHub <noreply@github.com>2022-12-22 07:12:17 +0100
commitae0899be0d787fbfc5f5ab2b18c5a8219d822d2b (patch)
treef20955a6a6569046d0257ad86295762e10ed1c62 /django/db/backends/postgresql/schema.py
parent3b24a3fa337bedc11352a17952cb9c3f77f9b9f2 (diff)
downloaddjango-ae0899be0d787fbfc5f5ab2b18c5a8219d822d2b.tar.gz
Fixed #34219 -- Preserved Char/TextField.db_collation when altering column type.
This moves setting a database collation to the column type alteration as both must be set at the same time. This should also avoid another layer of the column type alteration when adding database comments support (#18468).
Diffstat (limited to 'django/db/backends/postgresql/schema.py')
-rw-r--r--django/db/backends/postgresql/schema.py17
1 files changed, 12 insertions, 5 deletions
diff --git a/django/db/backends/postgresql/schema.py b/django/db/backends/postgresql/schema.py
index 1bd72bc0cb..2887071254 100644
--- a/django/db/backends/postgresql/schema.py
+++ b/django/db/backends/postgresql/schema.py
@@ -140,7 +140,9 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
return sequence["name"]
return None
- def _alter_column_type_sql(self, model, old_field, new_field, new_type):
+ def _alter_column_type_sql(
+ self, model, old_field, new_field, new_type, old_collation, new_collation
+ ):
# Drop indexes on varchar/text/citext columns that are changing to a
# different type.
old_db_params = old_field.db_parameters(connection=self.connection)
@@ -155,7 +157,9 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
)
self.execute(self._delete_index_sql(model, index_name))
- self.sql_alter_column_type = "ALTER COLUMN %(column)s TYPE %(type)s"
+ self.sql_alter_column_type = (
+ "ALTER COLUMN %(column)s TYPE %(type)s%(collation)s"
+ )
# Cast when data type changed.
if using_sql := self._using_sql(new_field, old_field):
self.sql_alter_column_type += using_sql
@@ -178,6 +182,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
% {
"column": self.quote_name(column),
"type": new_type,
+ "collation": "",
},
[],
),
@@ -204,7 +209,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
)
column = strip_quotes(new_field.column)
fragment, _ = super()._alter_column_type_sql(
- model, old_field, new_field, new_type
+ model, old_field, new_field, new_type, old_collation, new_collation
)
# Drop the sequence if exists (Django 4.1+ identity columns don't
# have it).
@@ -222,7 +227,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
return fragment, other_actions
elif new_is_auto and old_is_auto and old_internal_type != new_internal_type:
fragment, _ = super()._alter_column_type_sql(
- model, old_field, new_field, new_type
+ model, old_field, new_field, new_type, old_collation, new_collation
)
column = strip_quotes(new_field.column)
db_types = {
@@ -246,7 +251,9 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
]
return fragment, other_actions
else:
- return super()._alter_column_type_sql(model, old_field, new_field, new_type)
+ return super()._alter_column_type_sql(
+ model, old_field, new_field, new_type, old_collation, new_collation
+ )
def _alter_column_collation_sql(
self, model, new_field, new_type, new_collation, old_field