Fixed #33901 -- Skipped varchar_pattern_ops/text_pattern_ops index creation when db_collation is set.

This commit is contained in:
Haolun Chai 2022-08-14 05:12:56 -04:00 committed by Mariusz Felisiak
parent e30d667842
commit f3f9d03edf
2 changed files with 40 additions and 0 deletions

View File

@ -96,6 +96,10 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
# and text[size], so skip them. # and text[size], so skip them.
if "[" in db_type: if "[" in db_type:
return None return None
# Non-deterministic collations on Postgresql don't support indexes
# for operator classes varchar_pattern_ops/text_pattern_ops.
if getattr(field, "db_collation", None):
return None
if db_type.startswith("varchar"): if db_type.startswith("varchar"):
return self._create_index_sql( return self._create_index_sql(
model, model,

View File

@ -1321,6 +1321,42 @@ class SchemaTests(TransactionTestCase):
cs_collation, cs_collation,
) )
@isolate_apps("schema")
@unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
@skipUnlessDBFeature(
"supports_collation_on_charfield",
"supports_non_deterministic_collations",
)
def test_unique_with_collation_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)
class CiCharModel(Model):
field = CharField(max_length=16, db_collation=ci_collation, unique=True)
class Meta:
app_label = "schema"
# Create the table.
with connection.schema_editor() as editor:
editor.create_model(CiCharModel)
self.isolated_local_models = [CiCharModel]
self.assertEqual(
self.get_column_collation(CiCharModel._meta.db_table, "field"),
ci_collation,
)
self.assertIn("field", self.get_uniques(CiCharModel._meta.db_table))
def test_alter_textfield_to_null(self): def test_alter_textfield_to_null(self):
""" """
#24307 - Should skip an alter statement on databases with #24307 - Should skip an alter statement on databases with