Ticket #6475: django-sql-constraints-fix.diff

File django-sql-constraints-fix.diff, 3.3 KB (added by railk, 17 years ago)

fix for commented-out constraints. my python is rusty, so may need cleanup.

  • django/core/management/sql.py

     
    9090        final_output.extend(output)
    9191        for refto, refs in references.items():
    9292            pending_references.setdefault(refto, []).extend(refs)
    93         final_output.extend(sql_for_pending_references(model, style, pending_references))
     93        final_output.extend(sql_for_pending_references(model, style, pending_references, known_models))
    9494        # Keep track of the fact that we've created the table for this model.
    9595        known_models.add(model)
    9696
     
    314314
    315315    return final_output, pending_references
    316316
    317 def sql_for_pending_references(model, style, pending_references):
     317def sql_for_pending_references(model, style, pending_references, known_models=[]):
    318318    """
    319319    Returns any ALTER TABLE statements to add constraints after the fact.
    320320    """
     
    324324    qn = connection.ops.quote_name
    325325    final_output = []
    326326    if connection.features.supports_constraints:
    327         opts = model._meta
     327        for src_class in pending_references:
     328            opts = src_class._meta
     329            rels = pending_references[src_class]
     330            i = 0
     331            while i < len(rels):
     332                rel_class, f = rels[i]
     333                if src_class == model or (known_models and rel_class == model and (src_class in known_models)):
     334                    rel_opts = rel_class._meta
     335                    r_table = rel_opts.db_table
     336                    r_col = f.column
     337                    table = opts.db_table
     338                    col = opts.get_field(f.rel.field_name).column
     339                    # For MySQL, r_name must be unique in the first 64 characters.
     340                    # So we are careful with character usage here.
     341                    r_name = '%s_refs_%s_%x' % (r_col, col, abs(hash((r_table, table))))
     342                    final_output.append(style.SQL_KEYWORD('ALTER TABLE') + ' %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s;' % \
     343                        (qn(r_table), truncate_name(r_name, connection.ops.max_name_length()),
     344                        qn(r_col), qn(table), qn(col),
     345                        connection.ops.deferrable_sql()))
     346                    del rels[i]
     347                else:
     348                    i = i + 1
    328349        if model in pending_references:
    329             for rel_class, f in pending_references[model]:
    330                 rel_opts = rel_class._meta
    331                 r_table = rel_opts.db_table
    332                 r_col = f.column
    333                 table = opts.db_table
    334                 col = opts.get_field(f.rel.field_name).column
    335                 # For MySQL, r_name must be unique in the first 64 characters.
    336                 # So we are careful with character usage here.
    337                 r_name = '%s_refs_%s_%x' % (r_col, col, abs(hash((r_table, table))))
    338                 final_output.append(style.SQL_KEYWORD('ALTER TABLE') + ' %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s;' % \
    339                     (qn(r_table), truncate_name(r_name, connection.ops.max_name_length()),
    340                     qn(r_col), qn(table), qn(col),
    341                     connection.ops.deferrable_sql()))
    342350            del pending_references[model]
    343351    return final_output
    344352
Back to Top