[Django] #34898: Adding non-deterministic collations to unique CharFields crashes on PostgreSQL.

8 views
Skip to first unread message

Django

unread,
Oct 10, 2023, 11:54:25 PM10/10/23
to django-...@googlegroups.com
#34898: Adding non-deterministic collations to unique CharFields crashes on
PostgreSQL.
-------------------------------------+-------------------------------------
Reporter: Mariusz | Owner: nobody
Felisiak |
Type: Bug | Status: new
Component: | Version: 4.2
Migrations | Keywords: PostgreSQL
Severity: Normal | collation
Triage Stage: Accepted | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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].

--
Ticket URL: <https://code.djangoproject.com/ticket/34898>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Reply all
Reply to author
Forward
0 new messages