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 |
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())) |