summaryrefslogtreecommitdiff
path: root/django/db/backends/postgresql
diff options
context:
space:
mode:
authorTom Carrick <tom@carrick.eu>2023-10-27 15:46:05 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-10-30 07:37:40 +0100
commit34b411762b50883d768d7b67e0a158ec39da8b09 (patch)
treea8bdec327436eb7be76b1219e61fdcdfc1735b1a /django/db/backends/postgresql
parent46df3ab244e1688bd186f0bfbfea6a354097a910 (diff)
Fixed #34932 -- Restored varchar_pattern_ops/text_pattern_ops index creation when deterministic collaction is set.
Regression in f3f9d03edf17ccfa17263c7efa0b1350d1ac9278 (4.2) and 8ed25d65ea7546fafd808086fa07e7e5bb5428fc (5.0).
Diffstat (limited to 'django/db/backends/postgresql')
-rw-r--r--django/db/backends/postgresql/features.py1
-rw-r--r--django/db/backends/postgresql/schema.py20
2 files changed, 18 insertions, 3 deletions
diff --git a/django/db/backends/postgresql/features.py b/django/db/backends/postgresql/features.py
index f2c3800307..0fe5d950a5 100644
--- a/django/db/backends/postgresql/features.py
+++ b/django/db/backends/postgresql/features.py
@@ -74,6 +74,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
supports_virtual_generated_columns = False
can_rename_index = True
test_collations = {
+ "deterministic": "C",
"non_default": "sv-x-icu",
"swedish_ci": "sv-x-icu",
}
diff --git a/django/db/backends/postgresql/schema.py b/django/db/backends/postgresql/schema.py
index 40fef6660e..5dc93a27d0 100644
--- a/django/db/backends/postgresql/schema.py
+++ b/django/db/backends/postgresql/schema.py
@@ -98,9 +98,10 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
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) or (
- field.is_relation and getattr(field.target_field, "db_collation", None)
- ):
+ collation_name = getattr(field, "db_collation", None)
+ if not collation_name and field.is_relation:
+ collation_name = getattr(field.target_field, "db_collation", None)
+ if collation_name and not self._is_collation_deterministic(collation_name):
return None
if db_type.startswith("varchar"):
return self._create_index_sql(
@@ -372,3 +373,16 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
include=include,
expressions=expressions,
)
+
+ def _is_collation_deterministic(self, collation_name):
+ with self.connection.cursor() as cursor:
+ cursor.execute(
+ """
+ SELECT collisdeterministic
+ FROM pg_collation
+ WHERE collname = %s
+ """,
+ [collation_name],
+ )
+ row = cursor.fetchone()
+ return row[0] if row else None