Kaydet (Commit) e21a4c8d authored tarafından Russell Keith-Magee's avatar Russell Keith-Magee

Fixed #3779 -- Resolved problem with order of creation of m2m tables during…

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.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@4780 bcc190cf-cafb-0310-a4f2-bffc1f526a37
üst 6a7a1558
...@@ -280,7 +280,7 @@ def get_sql_delete(app): ...@@ -280,7 +280,7 @@ def get_sql_delete(app):
from django.db import backend, connection, models, get_introspection_module from django.db import backend, connection, models, get_introspection_module
introspection = 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: try:
cursor = connection.cursor() cursor = connection.cursor()
except: except:
...@@ -512,6 +512,7 @@ def syncdb(verbosity=1, interactive=True): ...@@ -512,6 +512,7 @@ def syncdb(verbosity=1, interactive=True):
created_models = set() created_models = set()
pending_references = {} pending_references = {}
# Create the tables for each model
for app in models.get_apps(): for app in models.get_apps():
app_name = app.__name__.split('.')[-2] app_name = app.__name__.split('.')[-2]
model_list = models.get_models(app) model_list = models.get_models(app)
...@@ -533,6 +534,11 @@ def syncdb(verbosity=1, interactive=True): ...@@ -533,6 +534,11 @@ def syncdb(verbosity=1, interactive=True):
cursor.execute(statement) cursor.execute(statement)
table_list.append(model._meta.db_table) 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: for model in model_list:
if model in created_models: if model in created_models:
sql = _get_many_to_many_sql_for_model(model) sql = _get_many_to_many_sql_for_model(model)
...@@ -542,7 +548,7 @@ def syncdb(verbosity=1, interactive=True): ...@@ -542,7 +548,7 @@ def syncdb(verbosity=1, interactive=True):
for statement in sql: for statement in sql:
cursor.execute(statement) cursor.execute(statement)
transaction.commit_unless_managed() transaction.commit_unless_managed()
# Send the post_syncdb signal, so individual apps can do whatever they need # Send the post_syncdb signal, so individual apps can do whatever they need
# to do at this point. # to do at this point.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment