Fixed #2257 -- make constraint names unique across all table/column
combinations (for the benefit of MySQL). git-svn-id: http://code.djangoproject.com/svn/django/trunk@3512 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
a7f8984e6e
commit
fa5e0562dc
|
@ -192,7 +192,6 @@ def _get_sql_for_pending_references(model, pending_references):
|
||||||
data_types = get_creation_module().DATA_TYPES
|
data_types = get_creation_module().DATA_TYPES
|
||||||
|
|
||||||
final_output = []
|
final_output = []
|
||||||
reference_names = {}
|
|
||||||
if backend.supports_constraints:
|
if backend.supports_constraints:
|
||||||
opts = model._meta
|
opts = model._meta
|
||||||
if model in pending_references:
|
if model in pending_references:
|
||||||
|
@ -202,12 +201,9 @@ def _get_sql_for_pending_references(model, pending_references):
|
||||||
r_col = f.column
|
r_col = f.column
|
||||||
table = opts.db_table
|
table = opts.db_table
|
||||||
col = opts.get_field(f.rel.field_name).column
|
col = opts.get_field(f.rel.field_name).column
|
||||||
r_name = '%s_referencing_%s_%s' % (r_col, table, col)
|
# For MySQL, r_name must be unique in the first 64 characters.
|
||||||
if r_name in reference_names:
|
# So we are careful with character usage here.
|
||||||
reference_names[r_name] += 1
|
r_name = '%s_refs_%s_%x' % (r_col, col, abs(hash((r_table, table))))
|
||||||
r_name += '_%s' % reference_names[r_name]
|
|
||||||
else:
|
|
||||||
reference_names[r_name] = 0
|
|
||||||
final_output.append(style.SQL_KEYWORD('ALTER TABLE') + ' %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s);' % \
|
final_output.append(style.SQL_KEYWORD('ALTER TABLE') + ' %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s);' % \
|
||||||
(backend.quote_name(r_table), r_name,
|
(backend.quote_name(r_table), r_name,
|
||||||
backend.quote_name(r_col), backend.quote_name(table), backend.quote_name(col)))
|
backend.quote_name(r_col), backend.quote_name(table), backend.quote_name(col)))
|
||||||
|
|
Loading…
Reference in New Issue