Commits

Anonymous committed b690c6a

Fixed #3779 -- Resolved problem with order of creation of m2m tables during syncdb. Well spotted, Ben Slavin, and thanks for the very helpful test case.

Comments (0)

Files changed (1)

django/core/management.py

     from django.db import backend, connection, models, get_introspection_module
     introspection = get_introspection_module()
 
-    # This should work even if a connecton isn't available
+    # This should work even if a connection isn't available
     try:
         cursor = connection.cursor()
     except:
     created_models = set()
     pending_references = {}
 
+    # Create the tables for each model
     for app in models.get_apps():
         app_name = app.__name__.split('.')[-2]
         model_list = models.get_models(app)
                 cursor.execute(statement)
             table_list.append(model._meta.db_table)
 
+    # Create the m2m tables. This must be done after all tables have been created
+    # to ensure that all referred tables will exist.
+    for app in models.get_apps():
+        app_name = app.__name__.split('.')[-2]
+        model_list = models.get_models(app)
         for model in model_list:
             if model in created_models:
                 sql = _get_many_to_many_sql_for_model(model)
                     for statement in sql:
                         cursor.execute(statement)
 
-        transaction.commit_unless_managed()
+    transaction.commit_unless_managed()
 
     # Send the post_syncdb signal, so individual apps can do whatever they need
     # to do at this point.
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 ProjectModifiedEvent.java.
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.