diff --git a/django/core/management.py b/django/core/management.py index b5ed60d2c5..043915f9e5 100644 --- a/django/core/management.py +++ b/django/core/management.py @@ -117,18 +117,19 @@ def get_sql_create(app): # Take care of any ALTER TABLE statements to add constraints # after the fact. - if klass in pending_references: - for rel_class, f in pending_references[klass]: - rel_opts = rel_class._meta - r_table = rel_opts.db_table - r_col = f.column - table = opts.db_table - col = opts.get_field(f.rel.field_name).column - final_output.append('ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s);' % \ - (backend.quote_name(r_table), - backend.quote_name("%s_referencing_%s_%s" % (r_col, table, col)), - backend.quote_name(r_col), backend.quote_name(table), backend.quote_name(col))) - del pending_references[klass] + if backend.supports_constraints: + if klass in pending_references: + for rel_class, f in pending_references[klass]: + rel_opts = rel_class._meta + r_table = rel_opts.db_table + r_col = f.column + table = opts.db_table + col = opts.get_field(f.rel.field_name).column + final_output.append('ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s);' % \ + (backend.quote_name(r_table), + backend.quote_name("%s_referencing_%s_%s" % (r_col, table, col)), + backend.quote_name(r_col), backend.quote_name(table), backend.quote_name(col))) + del pending_references[klass] # Keep track of the fact that we've created the table for this model. models_output.add(klass) diff --git a/django/db/backends/ado_mssql/base.py b/django/db/backends/ado_mssql/base.py index 982cea40a0..3fbcc171c6 100644 --- a/django/db/backends/ado_mssql/base.py +++ b/django/db/backends/ado_mssql/base.py @@ -76,6 +76,8 @@ class DatabaseWrapper: self.connection.close() self.connection = None +supports_constraints = True + def quote_name(name): if name.startswith('[') and name.endswith(']'): return name # Quoting once is enough. diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py index 32476c0f7b..de3c250e4a 100644 --- a/django/db/backends/mysql/base.py +++ b/django/db/backends/mysql/base.py @@ -83,6 +83,8 @@ class DatabaseWrapper: self.connection.close() self.connection = None +supports_constraints = True + def quote_name(name): if name.startswith("`") and name.endswith("`"): return name # Quoting once is enough. diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py index 482fa2a5f7..bc16564a3b 100644 --- a/django/db/backends/postgresql/base.py +++ b/django/db/backends/postgresql/base.py @@ -49,6 +49,8 @@ class DatabaseWrapper: self.connection.close() self.connection = None +supports_constraints = True + def quote_name(name): if name.startswith('"') and name.endswith('"'): return name # Quoting once is enough. diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py index a5bc6a4149..0eb166d7fb 100644 --- a/django/db/backends/sqlite3/base.py +++ b/django/db/backends/sqlite3/base.py @@ -70,6 +70,8 @@ class SQLiteCursorWrapper(Database.Cursor): # XXX this seems too simple to be correct... is this right? return query % tuple("?" * num_params) +supports_constraints = False + def quote_name(name): if name.startswith('"') and name.endswith('"'): return name # Quoting once is enough.