diff --git a/django/core/management/commands/sqlcustom.py b/django/core/management/commands/sqlcustom.py index 90b90b6d49..465330db7e 100644 --- a/django/core/management/commands/sqlcustom.py +++ b/django/core/management/commands/sqlcustom.py @@ -7,4 +7,4 @@ class Command(AppCommand): def handle_app(self, app, **options): from django.core.management.sql import sql_custom - return u'\n'.join(sql_custom(app)).encode('utf-8') + return u'\n'.join(sql_custom(app, self.style)).encode('utf-8') diff --git a/django/core/management/commands/syncdb.py b/django/core/management/commands/syncdb.py index 3792a312d7..38d1c91abd 100644 --- a/django/core/management/commands/syncdb.py +++ b/django/core/management/commands/syncdb.py @@ -116,7 +116,7 @@ class Command(NoArgsCommand): app_name = app.__name__.split('.')[-2] for model in models.get_models(app): if model in created_models: - custom_sql = custom_sql_for_model(model) + custom_sql = custom_sql_for_model(model, self.style) if custom_sql: if verbosity >= 1: print "Installing custom SQL for %s.%s model" % (app_name, model._meta.object_name) diff --git a/django/core/management/sql.py b/django/core/management/sql.py index 000886ada5..86e9ed325f 100644 --- a/django/core/management/sql.py +++ b/django/core/management/sql.py @@ -219,7 +219,7 @@ def sql_flush(style, only_django=False): statements = connection.ops.sql_flush(style, tables, sequence_list()) return statements -def sql_custom(app): +def sql_custom(app, style): "Returns a list of the custom table modifying SQL statements for the given app." from django.db.models import get_models output = [] @@ -228,7 +228,7 @@ def sql_custom(app): app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql')) for model in app_models: - output.extend(custom_sql_for_model(model)) + output.extend(custom_sql_for_model(model, style)) return output @@ -242,7 +242,7 @@ def sql_indexes(app, style): def sql_all(app, style): "Returns a list of CREATE TABLE SQL, initial-data inserts, and CREATE INDEX SQL for the given module." - return sql_create(app, style) + sql_custom(app) + sql_indexes(app, style) + return sql_create(app, style) + sql_custom(app, style) + sql_indexes(app, style) def sql_model_create(model, style, known_models=set()): """ @@ -426,7 +426,7 @@ def many_to_many_sql_for_model(model, style): return final_output -def custom_sql_for_model(model): +def custom_sql_for_model(model, style): from django.db import models from django.conf import settings @@ -434,6 +434,14 @@ def custom_sql_for_model(model): app_dir = os.path.normpath(os.path.join(os.path.dirname(models.get_app(model._meta.app_label).__file__), 'sql')) output = [] + # Post-creation SQL should come before any initial SQL data is loaded. + # However, this should not be done for fields that are part of a a parent + # model (via model inheritance). + nm = opts.init_name_map() + post_sql_fields = [f for f in opts.local_fields if hasattr(f, 'post_create_sql')] + for f in post_sql_fields: + output.extend(f.post_create_sql(style, model._meta.db_table)) + # Some backends can't execute more than one SQL statement at a time, # so split into separate statements. statements = re.compile(r";[ \t]*$", re.M)