﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34898	Adding non-deterministic collations to unique CharFields crashes on PostgreSQL.	Mariusz Felisiak	Tom Carrick	"Adding non-deterministic collations to unique `CharField`s crashes on PostgreSQL. A regression test:

{{{#!diff
diff --git a/tests/schema/tests.py b/tests/schema/tests.py
index 68b6442794..ba8d9e99ec 100644
--- a/tests/schema/tests.py
+++ b/tests/schema/tests.py
@@ -1402,6 +1402,44 @@ class SchemaTests(TransactionTestCase):
         )
         self.assertIn(""field"", self.get_uniques(CiCharModel._meta.db_table))
 
+    @unittest.skipUnless(connection.vendor == ""postgresql"", ""PostgreSQL specific"")
+    @skipUnlessDBFeature(
+        ""supports_collation_on_charfield"",
+        ""supports_non_deterministic_collations"",
+    )
+    def test_add_collation_to_unique_charfield(self):
+        ci_collation = ""case_insensitive""
+
+        def drop_collation():
+            with connection.cursor() as cursor:
+                cursor.execute(f""DROP COLLATION IF EXISTS {ci_collation}"")
+
+        with connection.cursor() as cursor:
+            cursor.execute(
+                f""CREATE COLLATION IF NOT EXISTS {ci_collation} (provider = icu, ""
+                f""locale = 'und-u-ks-level2', deterministic = false)""
+            )
+        self.addCleanup(drop_collation)
+
+        # Create the table.
+        with connection.schema_editor() as editor:
+            editor.create_model(AuthorWithUniqueName)
+        # Add db_collation.
+        old_field = AuthorWithUniqueName._meta.get_field(""name"")
+        new_field = CharField(max_length=255, unique=True, db_collation=ci_collation)
+        new_field.model = AuthorWithUniqueName
+        new_field.set_attributes_from_name(""name"")
+        with connection.schema_editor() as editor:
+            editor.alter_field(
+                AuthorWithUniqueName, old_field, new_field, strict=True
+            )
+
+        self.assertEqual(
+            self.get_column_collation(AuthorWithUniqueName._meta.db_table, ""field""),
+            ci_collation,
+        )
+        self.assertIn(""field"", self.get_uniques(AuthorWithUniqueName._meta.db_table))
+
     @isolate_apps(""schema"")
     @unittest.skipUnless(connection.vendor == ""postgresql"", ""PostgreSQL specific"")
     @skipUnlessDBFeature(

}}}



[https://code.djangoproject.com/ticket/33901#comment:18 Reported by Adam]."	Bug	assigned	Migrations	4.2	Normal		PostgreSQL collation	Jacob Walls Peter Thomassen Ülgen Sarıkavak Yiwei Gao Sudarshan R	Accepted	0	0	0	0	0	0
