Anonymous avatar Anonymous committed cf7a974

[multi-db] Fixed bug in handling of inter-app pending statements on syncdb.

Comments (0)

Files changed (1)


     # Install each app
+    pending = None
     for app in models.get_apps():
         # Install each application (models already installed will be skipped)
-        created = _install(app, commit=False, initial_data=False)
+        created, pending = _install(app, commit=False, initial_data=False,
+                                    pending_allowed=True, pending=pending)
         if verbosity >= 2:
             for model in created:
                 print "Created table %s" % model._meta.db_table
+    if pending:
+        transaction.rollback_unless_managed()
+        raise Exception("All apps were installed, but there were still "
+                        "pending references to: " + ",".join(pending.keys()) +
+                        ". Transaction rolled back.")
     # Send the post_syncdb signal, so individual apps can do whatever they need
     # doesn't complain about unprintable output.    
-def _install(app, commit=True, initial_data=True):
+def _install(app, commit=True, initial_data=True, pending_allowed=False,
+             pending=None):
     from django.db import connection, models, transaction
     import sys
     created_models = []
-        pending = {}
+        if pending is None:
+            pending = {}
         for model in models.get_models(app, creation_order=True):
             manager = model._default_manager
             tables = manager.get_table_list()
                         for statement in manager.get_pending(rel_class, f):
-                else:
+                elif not pending_allowed:
                     raise Exception("%s is not installed, but it has pending "
                                     "references" % model)
     except Exception, e:
     if commit:
-    return created_models
+    return created_models, pending
 install.help_doc = "Executes ``sqlall`` for the given app(s) in the current database."
 install.args = APP_ARGS
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.