From deeffde84a23660e3dd589abaaa7454f4ee45fda Mon Sep 17 00:00:00 2001
From: Markus Holtermann <info@markusholtermann.eu>
Date: Fri, 6 May 2016 02:13:32 +0200
Subject: [PATCH] Fixed #26593 -- Leveraged deferrable_sql() in SchemaEditor

---
 django/db/backends/base/schema.py  | 3 ++-
 django/db/backends/mysql/schema.py | 4 ----
 tests/schema/tests.py              | 1 +
 3 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py
index 48791be510..0dcd71a9d0 100644
--- a/django/db/backends/base/schema.py
+++ b/django/db/backends/base/schema.py
@@ -58,7 +58,7 @@ class BaseDatabaseSchemaEditor(object):
 
     sql_create_fk = (
         "ALTER TABLE %(table)s ADD CONSTRAINT %(name)s FOREIGN KEY (%(column)s) "
-        "REFERENCES %(to_table)s (%(to_column)s) DEFERRABLE INITIALLY DEFERRED"
+        "REFERENCES %(to_table)s (%(to_column)s)%(deferrable)s"
     )
     sql_create_inline_fk = None
     sql_delete_fk = "ALTER TABLE %(table)s DROP CONSTRAINT %(name)s"
@@ -889,6 +889,7 @@ class BaseDatabaseSchemaEditor(object):
             "column": self.quote_name(from_column),
             "to_table": self.quote_name(to_table),
             "to_column": self.quote_name(to_column),
+            "deferrable": self.connection.ops.deferrable_sql(),
         }
 
     def _create_unique_sql(self, model, columns):
diff --git a/django/db/backends/mysql/schema.py b/django/db/backends/mysql/schema.py
index 3cdc53c858..802ce6353b 100644
--- a/django/db/backends/mysql/schema.py
+++ b/django/db/backends/mysql/schema.py
@@ -13,10 +13,6 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
 
     sql_delete_unique = "ALTER TABLE %(table)s DROP INDEX %(name)s"
 
-    sql_create_fk = (
-        "ALTER TABLE %(table)s ADD CONSTRAINT %(name)s FOREIGN KEY "
-        "(%(column)s) REFERENCES %(to_table)s (%(to_column)s)"
-    )
     sql_delete_fk = "ALTER TABLE %(table)s DROP FOREIGN KEY %(name)s"
 
     sql_delete_index = "DROP INDEX %(name)s ON %(table)s"
diff --git a/tests/schema/tests.py b/tests/schema/tests.py
index 3a426cb74c..5e2bd8ef96 100644
--- a/tests/schema/tests.py
+++ b/tests/schema/tests.py
@@ -1628,6 +1628,7 @@ class SchemaTests(TransactionTestCase):
                     "column": editor.quote_name(column),
                     "to_table": editor.quote_name(table),
                     "to_column": editor.quote_name(model._meta.auto_field.column),
+                    "deferrable": connection.ops.deferrable_sql(),
                 }
             )
             editor.alter_field(model, get_field(Author, CASCADE, field_class=ForeignKey), field)