Skip to content

Commit e21a4c8

Browse files
committed
Fixed django#3779 -- Resolved problem with order of creation of m2m tables during syncdb. Well spotted, Ben Slavin, and thanks for the very helpful test case.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4780 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 6a7a155 commit e21a4c8

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

django/core/management.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def get_sql_delete(app):
280280
from django.db import backend, connection, models, get_introspection_module
281281
introspection = get_introspection_module()
282282

283-
# This should work even if a connecton isn't available
283+
# This should work even if a connection isn't available
284284
try:
285285
cursor = connection.cursor()
286286
except:
@@ -512,6 +512,7 @@ def syncdb(verbosity=1, interactive=True):
512512
created_models = set()
513513
pending_references = {}
514514

515+
# Create the tables for each model
515516
for app in models.get_apps():
516517
app_name = app.__name__.split('.')[-2]
517518
model_list = models.get_models(app)
@@ -533,6 +534,11 @@ def syncdb(verbosity=1, interactive=True):
533534
cursor.execute(statement)
534535
table_list.append(model._meta.db_table)
535536

537+
# Create the m2m tables. This must be done after all tables have been created
538+
# to ensure that all referred tables will exist.
539+
for app in models.get_apps():
540+
app_name = app.__name__.split('.')[-2]
541+
model_list = models.get_models(app)
536542
for model in model_list:
537543
if model in created_models:
538544
sql = _get_many_to_many_sql_for_model(model)
@@ -542,7 +548,7 @@ def syncdb(verbosity=1, interactive=True):
542548
for statement in sql:
543549
cursor.execute(statement)
544550

545-
transaction.commit_unless_managed()
551+
transaction.commit_unless_managed()
546552

547553
# Send the post_syncdb signal, so individual apps can do whatever they need
548554
# to do at this point.

0 commit comments

Comments
 (0)