From 9d8492c169d54b727ab387132456d4c1526cff06 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Wed, 28 Apr 2010 12:08:30 +0000 Subject: [PATCH] Fixed #12977 -- Ensure that indexes don't exceed character limits on MySQL. Thanks to carljm for the original report, and hgeerts@osso.nl for the patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@13040 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/backends/creation.py | 5 ++++- django/db/backends/mysql/base.py | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/django/db/backends/creation.py b/django/db/backends/creation.py index aea00ea7f4b..e3a9ce0272c 100644 --- a/django/db/backends/creation.py +++ b/django/db/backends/creation.py @@ -260,6 +260,8 @@ class BaseDatabaseCreation(object): def sql_indexes_for_field(self, model, f, style): "Return the CREATE INDEX SQL statements for a single model field" + from django.db.backends.util import truncate_name + if f.db_index and not f.unique: qn = self.connection.ops.quote_name tablespace = f.db_tablespace or model._meta.db_tablespace @@ -271,8 +273,9 @@ class BaseDatabaseCreation(object): tablespace_sql = '' else: tablespace_sql = '' + i_name = '%s_%s' % (model._meta.db_table, self._digest(f.column)) output = [style.SQL_KEYWORD('CREATE INDEX') + ' ' + - style.SQL_TABLE(qn('%s_%s' % (model._meta.db_table, f.column))) + ' ' + + style.SQL_TABLE(qn(truncate_name(i_name, self.connection.ops.max_name_length()))) + ' ' + style.SQL_KEYWORD('ON') + ' ' + style.SQL_TABLE(qn(model._meta.db_table)) + ' ' + "(%s)" % style.SQL_FIELD(qn(f.column)) + diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py index 2ade58bc655..e94e24bff94 100644 --- a/django/db/backends/mysql/base.py +++ b/django/db/backends/mysql/base.py @@ -227,6 +227,9 @@ class DatabaseOperations(BaseDatabaseOperations): second = '%s-12-31 23:59:59.99' return [first % value, second % value] + def max_name_length(self): + return 64 + class DatabaseWrapper(BaseDatabaseWrapper): operators = {