Skip to content

Commit

Permalink
Fixed #12977 -- Ensure that indexes don't exceed character limits on …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
freakboy3742 committed Apr 28, 2010
1 parent 4e0aa65 commit 9d8492c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
5 changes: 4 additions & 1 deletion django/db/backends/creation.py
Expand Up @@ -260,6 +260,8 @@ def sql_indexes_for_model(self, model, style):


def sql_indexes_for_field(self, model, f, style): def sql_indexes_for_field(self, model, f, style):
"Return the CREATE INDEX SQL statements for a single model field" "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: if f.db_index and not f.unique:
qn = self.connection.ops.quote_name qn = self.connection.ops.quote_name
tablespace = f.db_tablespace or model._meta.db_tablespace tablespace = f.db_tablespace or model._meta.db_tablespace
Expand All @@ -271,8 +273,9 @@ def sql_indexes_for_field(self, model, f, style):
tablespace_sql = '' tablespace_sql = ''
else: else:
tablespace_sql = '' tablespace_sql = ''
i_name = '%s_%s' % (model._meta.db_table, self._digest(f.column))
output = [style.SQL_KEYWORD('CREATE INDEX') + ' ' + 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_KEYWORD('ON') + ' ' +
style.SQL_TABLE(qn(model._meta.db_table)) + ' ' + style.SQL_TABLE(qn(model._meta.db_table)) + ' ' +
"(%s)" % style.SQL_FIELD(qn(f.column)) + "(%s)" % style.SQL_FIELD(qn(f.column)) +
Expand Down
3 changes: 3 additions & 0 deletions django/db/backends/mysql/base.py
Expand Up @@ -227,6 +227,9 @@ def year_lookup_bounds(self, value):
second = '%s-12-31 23:59:59.99' second = '%s-12-31 23:59:59.99'
return [first % value, second % value] return [first % value, second % value]


def max_name_length(self):
return 64

class DatabaseWrapper(BaseDatabaseWrapper): class DatabaseWrapper(BaseDatabaseWrapper):


operators = { operators = {
Expand Down

1 comment on commit 9d8492c

@james4388
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey django 1.4.3 meet this bug again :(

Please sign in to comment.