From a1e4e86f923dc8387b0a9c3025bdd5d096a6ebb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandru=20M=C4=83r=C4=83=C8=99teanu?= Date: Fri, 15 Apr 2022 23:00:28 +0300 Subject: [PATCH] Fixed #33607 -- Made PostgresIndex.create_sql() respect the "using" argument. --- django/contrib/postgres/indexes.py | 2 +- tests/postgres_tests/test_indexes.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/django/contrib/postgres/indexes.py b/django/contrib/postgres/indexes.py index a0c03681b2..97377d8cb1 100644 --- a/django/contrib/postgres/indexes.py +++ b/django/contrib/postgres/indexes.py @@ -25,7 +25,7 @@ class PostgresIndex(Index): def create_sql(self, model, schema_editor, using="", **kwargs): self.check_supported(schema_editor) statement = super().create_sql( - model, schema_editor, using=" USING %s" % self.suffix, **kwargs + model, schema_editor, using=" USING %s" % (using or self.suffix), **kwargs ) with_params = self.get_with_params() if with_params: diff --git a/tests/postgres_tests/test_indexes.py b/tests/postgres_tests/test_indexes.py index 2f74203e8b..9da2f33ae8 100644 --- a/tests/postgres_tests/test_indexes.py +++ b/tests/postgres_tests/test_indexes.py @@ -8,6 +8,7 @@ from django.contrib.postgres.indexes import ( GistIndex, HashIndex, OpClass, + PostgresIndex, SpGistIndex, ) from django.db import NotSupportedError, connection @@ -646,6 +647,21 @@ class SchemaTests(PostgreSQLTestCase): editor.add_index(Scene, index) self.assertNotIn(index_name, self.get_constraints(Scene._meta.db_table)) + def test_custom_suffix(self): + class CustomSuffixIndex(PostgresIndex): + suffix = "sfx" + + def create_sql(self, model, schema_editor, using="gin", **kwargs): + return super().create_sql(model, schema_editor, using=using, **kwargs) + + index = CustomSuffixIndex(fields=["field"], name="custom_suffix_idx") + self.assertEqual(index.suffix, "sfx") + with connection.schema_editor() as editor: + self.assertIn( + " USING gin ", + str(index.create_sql(CharFieldModel, editor)), + ) + def test_op_class(self): index_name = "test_op_class" index = Index(