Opened 17 years ago

Closed 17 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 Wil Tan, 17 years ago

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)

comment:2 by Brian Rosner, 17 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #4193.

comment:3 by Jacob, 17 years ago

Resolution: duplicate
Status: closedreopened

Reopening since #4193 is more confusing than this ticket.

comment:4 by Jacob, 17 years ago

Owner: changed from nobody to Jacob
Status: reopenednew

comment:5 by Jacob, 17 years ago

Resolution: fixed
Status: newclosed

(In [7215]) Fixed #6374: cross-app and circular FK constraints are now detected and added correctly. Thanks, dready.

Note: See TracTickets for help on using tickets.
Back to Top