Ticket #9253: 9253-consistent_short_constraint_names.diff

File 9253-consistent_short_constraint_names.diff, 2.4 KB (added by Ramiro Morales, 16 years ago)

Patch from krzych updated to also fix sql_remove_table_constraints()

  • django/db/backends/creation.py

    diff -r 1a34cdd59170 django/db/backends/creation.py
    a b  
    11import sys
    22import time
     3from django.utils.hashcompat import md5_constructor
    34try:
    45    set
    56except NameError:
     
    126127                r_col = f.column
    127128                table = opts.db_table
    128129                col = opts.get_field(f.rel.field_name).column
    129                 # For MySQL, r_name must be unique in the first 64 characters.
     130                # For MySQL, r_name must be unique in the first 64 characters
     131                # and Oracle identifier lengths have a limit of 30 characters.
    130132                # So we are careful with character usage here.
    131                 r_name = '%s_refs_%s_%x' % (r_col, col, abs(hash((r_table, table))))
     133                r_name = '%s_refs_%s_%s' % (r_col, col,
     134                        md5_constructor(r_table + table).hexdigest()[:8])
    132135                final_output.append(style.SQL_KEYWORD('ALTER TABLE') + ' %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s;' % \
    133136                    (qn(r_table), qn(truncate_name(r_name, self.connection.ops.max_name_length())),
    134137                    qn(r_col), qn(table), qn(col),
     
    187190            output.append('\n'.join(table_output))
    188191
    189192            for r_table, r_col, table, col in deferred:
    190                 r_name = '%s_refs_%s_%x' % (r_col, col,
    191                         abs(hash((r_table, table))))
     193                r_name = '%s_refs_%s_%s' % (r_col, col,
     194                        md5_constructor(r_table + table).hexdigest()[:8])
    192195                output.append(style.SQL_KEYWORD('ALTER TABLE') + ' %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s;' %
    193196                (qn(r_table),
    194197                qn(truncate_name(r_name, self.connection.ops.max_name_length())),
     
    289292            col = f.column
    290293            r_table = model._meta.db_table
    291294            r_col = model._meta.get_field(f.rel.field_name).column
    292             r_name = '%s_refs_%s_%x' % (col, r_col, abs(hash((table, r_table))))
     295            r_name = '%s_refs_%s_%x' % (col, r_col,
     296                    md5_constructor(table + r_table).hexdigest()[:8])
    293297            output.append('%s %s %s %s;' % \
    294298                (style.SQL_KEYWORD('ALTER TABLE'),
    295299                style.SQL_TABLE(qn(table)),
Back to Top