summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Wobrock <david.wobrock@gmail.com>2022-04-28 17:23:15 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-04-29 06:30:15 +0200
commit87da2833384b7acd99a778a78e07ea6640f54f35 (patch)
tree2c317561b14ff358c7f3d8cd349d8cafbe8db080
parentaa28c392b9491f02330905cb73b7078b1cd18c60 (diff)
Refs #33413 -- Added collation to CharField/TextField's db_parameters.
-rw-r--r--django/db/backends/base/schema.py22
-rw-r--r--django/db/models/fields/__init__.py10
2 files changed, 24 insertions, 8 deletions
diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py
index 3a45b70311..696bfd7742 100644
--- a/django/db/backends/base/schema.py
+++ b/django/db/backends/base/schema.py
@@ -275,10 +275,11 @@ class BaseDatabaseSchemaEditor:
# Field <-> database mapping functions
- def _iter_column_sql(self, column_db_type, params, model, field, include_default):
+ def _iter_column_sql(
+ self, column_db_type, params, model, field, field_db_params, include_default
+ ):
yield column_db_type
- collation = getattr(field, "db_collation", None)
- if collation:
+ if collation := field_db_params.get("collation"):
yield self._collate_sql(collation)
# Work out nullability.
null = field.null
@@ -335,8 +336,8 @@ class BaseDatabaseSchemaEditor:
had set_attributes_from_name() called.
"""
# Get the column's type and use that as the basis of the SQL.
- db_params = field.db_parameters(connection=self.connection)
- column_db_type = db_params["type"]
+ field_db_params = field.db_parameters(connection=self.connection)
+ column_db_type = field_db_params["type"]
# Check for fields that aren't actually columns (e.g. M2M).
if column_db_type is None:
return None, None
@@ -345,7 +346,12 @@ class BaseDatabaseSchemaEditor:
" ".join(
# This appends to the params being returned.
self._iter_column_sql(
- column_db_type, params, model, field, include_default
+ column_db_type,
+ params,
+ model,
+ field,
+ field_db_params,
+ include_default,
)
),
params,
@@ -908,8 +914,8 @@ class BaseDatabaseSchemaEditor:
old_type_suffix = old_field.db_type_suffix(connection=self.connection)
new_type_suffix = new_field.db_type_suffix(connection=self.connection)
# Collation change?
- old_collation = getattr(old_field, "db_collation", None)
- new_collation = getattr(new_field, "db_collation", None)
+ old_collation = old_db_params.get("collation")
+ new_collation = new_db_params.get("collation")
if old_collation != new_collation:
# Collation change handles also a type change.
fragment = self._alter_column_collation_sql(
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index 5c5a5e0cfe..a073fce165 100644
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -1182,6 +1182,11 @@ class CharField(Field):
return connection.ops.cast_char_field_without_max_length
return super().cast_db_type(connection)
+ def db_parameters(self, connection):
+ db_params = super().db_parameters(connection)
+ db_params["collation"] = self.db_collation
+ return db_params
+
def get_internal_type(self):
return "CharField"
@@ -2361,6 +2366,11 @@ class TextField(Field):
)
return errors
+ def db_parameters(self, connection):
+ db_params = super().db_parameters(connection)
+ db_params["collation"] = self.db_collation
+ return db_params
+
def get_internal_type(self):
return "TextField"