Django

Code

Ticket #6374 (closed: fixed)

Opened 8 months ago

Last modified 6 months ago

Foreign key constraints not added across apps when creating tables

Reported by: dready Assigned to: jacob
Milestone: Component: Database wrapper
Version: SVN Keywords: mysql db models
Cc: Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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)

Attachments

Change History

01/13/08 14:56:28 changed by dready

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

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)

01/13/08 17:57:31 changed by brosner

  • status changed from new to closed.
  • resolution set to duplicate.

Duplicate of #4193.

02/20/08 15:33:32 changed by jacob

  • status changed from closed to reopened.
  • resolution deleted.

Reopening since #4193 is more confusing than this ticket.

03/09/08 17:12:27 changed by jacob

  • owner changed from nobody to jacob.
  • status changed from reopened to new.

03/10/08 17:18:50 changed by jacob

  • status changed from new to closed.
  • resolution set to fixed.

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


Add/Change #6374 (Foreign key constraints not added across apps when creating tables)




Change Properties
Action