From f37c11eea3cb860112a26f8a65d823842f65b96f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?La=CC=81szlo=CC=81=20Ka=CC=81rolyi?= Date: Wed, 25 Feb 2015 19:48:35 +0100 Subject: [PATCH] Fixed #24390 -- Made migration index names deterministic. --- django/db/backends/base/schema.py | 2 +- tests/indexes/tests.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py index b28e9c4ed5..96c7b4b7b8 100644 --- a/django/db/backends/base/schema.py +++ b/django/db/backends/base/schema.py @@ -798,7 +798,7 @@ class BaseDatabaseSchemaEditor(object): ) # Else generate the name for the index using a different algorithm table_name = model._meta.db_table.replace('"', '').replace('.', '_') - index_unique_name = '_%x' % abs(hash((table_name, ','.join(column_names)))) + index_unique_name = '_%s' % self._digest(*column_names) max_length = self.connection.ops.max_name_length() or 200 # If the index name is too long, truncate it index_name = ('%s_%s%s%s' % ( diff --git a/tests/indexes/tests.py b/tests/indexes/tests.py index 95de0a2963..749d6081fc 100644 --- a/tests/indexes/tests.py +++ b/tests/indexes/tests.py @@ -10,6 +10,19 @@ class SchemaIndexesTests(TestCase): """ Test index handling by the db.backends.schema infrastructure. """ + + def test_index_name_hash(self): + """ + Index names should be deterministic. + """ + with connection.schema_editor() as editor: + index_name = editor._create_index_name( + model=Article, + column_names=("column1", "column2", "column3"), + suffix="123", + ) + self.assertEqual(index_name, "indexes_article_column1_856fe518123") + def test_index_together(self): editor = connection.schema_editor() index_sql = editor._model_indexes_sql(Article)