Opened 18 years ago
Closed 18 years ago
#6374 closed (fixed)
Foreign key constraints not added across apps when creating tables
| Reported by: | Wil Tan | Owned by: | Jacob |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev |
| Severity: | Keywords: | mysql db models | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
I have an app that has foreign keys to django.contrib.auth.models.User but when doing 'syncdb' or 'sql', it doesn't define the foreign keys. My database engine is MySQL and I'm using SVN rev 7020.
I think this patch might work, but I'm in no way familiar with Django internals:
Index: django/core/management/sql.py
===================================================================
--- django/core/management/sql.py (revision 7020)
+++ django/core/management/sql.py (working copy)
@@ -90,6 +90,8 @@
final_output.extend(output)
for refto, refs in references.items():
pending_references.setdefault(refto, []).extend(refs)
+ if refto in known_models:
+ final_output.extend(sql_for_pending_references(refto, style, pending_references))
final_output.extend(sql_for_pending_references(model, style, pending_references))
# Keep track of the fact that we've created the table for this model.
known_models.add(model)
Change History (5)
comment:1 by , 18 years ago
comment:3 by , 18 years ago
| Resolution: | duplicate |
|---|---|
| Status: | closed → reopened |
Reopening since #4193 is more confusing than this ticket.
comment:4 by , 18 years ago
| Owner: | changed from to |
|---|---|
| Status: | reopened → new |
comment:5 by , 18 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
Looks like 'syncdb' has the same problem too so I've included the patch to syncdb as well:
Index: django/core/management/commands/syncdb.py =================================================================== --- django/core/management/commands/syncdb.py (revision 7020) +++ django/core/management/commands/syncdb.py (working copy) @@ -67,6 +67,8 @@ created_models.add(model) for refto, refs in references.items(): pending_references.setdefault(refto, []).extend(refs) + if refto in seen_models: + sql.extend(sql_for_pending_references(refto, self.style, pending_references)) sql.extend(sql_for_pending_references(model, self.style, pending_references)) if verbosity >= 1: print "Creating table %s" % model._meta.db_table Index: django/core/management/sql.py =================================================================== --- django/core/management/sql.py (revision 7020) +++ django/core/management/sql.py (working copy) @@ -90,6 +90,8 @@ final_output.extend(output) for refto, refs in references.items(): pending_references.setdefault(refto, []).extend(refs) + if refto in known_models: + final_output.extend(sql_for_pending_references(refto, style, pending_references)) final_output.extend(sql_for_pending_references(model, style, pending_references)) # Keep track of the fact that we've created the table for this model. known_models.add(model)