Opened 7 years ago

Closed 7 years ago

#6374 closed (fixed)

Foreign key constraints not added across apps when creating tables

Reported by: dready Owned by: jacob
Component: Database layer (models, ORM) Version: master
Severity: Keywords: mysql db models
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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 Changed 7 years ago by dready

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 Changed 7 years ago by brosner

  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #4193.

comment:3 Changed 7 years ago by jacob

  • Resolution duplicate deleted
  • Status changed from closed to reopened

Reopening since #4193 is more confusing than this ticket.

comment:4 Changed 7 years ago by jacob

  • Owner changed from nobody to jacob
  • Status changed from reopened to new

comment:5 Changed 7 years ago by jacob

  • Resolution set to fixed
  • Status changed from new to closed

(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