瀏覽代碼

Added generation of SQLite FK DDL in initial migrations.

SQLite accepts the relevant standard SQL (although by default it doesn't
enforce the constraint), and the 'traditional' creation backend helper
generate it, so this allows us to:

- Maintain the status quo
- Improve readability of the SQL code generated for that backend.

Also, we will need this for when we fix Refs #14204.
Ramiro Morales 11 年之前
父節點
當前提交
5782c94f23
共有 2 個文件被更改,包括 20 次插入12 次删除
  1. 19 12
      django/db/backends/schema.py
  2. 1 0
      django/db/backends/sqlite3/schema.py

+ 19 - 12
django/db/backends/schema.py

@@ -52,6 +52,7 @@ class BaseDatabaseSchemaEditor(object):
     sql_delete_unique = "ALTER TABLE %(table)s DROP CONSTRAINT %(name)s"
 
     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"
+    sql_create_inline_fk = None
     sql_delete_fk = "ALTER TABLE %(table)s DROP CONSTRAINT %(name)s"
 
     sql_create_index = "CREATE INDEX %(name)s ON %(table)s (%(columns)s)%(extra)s"
@@ -189,11 +190,6 @@ class BaseDatabaseSchemaEditor(object):
             col_type_suffix = field.db_type_suffix(connection=self.connection)
             if col_type_suffix:
                 definition += " %s" % col_type_suffix
-            # Add the SQL to our big list
-            column_sqls.append("%s %s" % (
-                self.quote_name(field.column),
-                definition,
-            ))
             params.extend(extra_params)
             # Indexes
             if field.db_index and not field.unique:
@@ -206,18 +202,29 @@ class BaseDatabaseSchemaEditor(object):
                     }
                 )
             # FK
-            if field.rel and self.connection.features.supports_foreign_keys:
+            if field.rel:
                 to_table = field.rel.to._meta.db_table
                 to_column = field.rel.to._meta.get_field(field.rel.field_name).column
-                self.deferred_sql.append(
-                    self.sql_create_fk % {
-                        "name": self._create_index_name(model, [field.column], suffix="_fk_%s_%s" % (to_table, to_column)),
-                        "table": self.quote_name(model._meta.db_table),
-                        "column": self.quote_name(field.column),
+                if self.connection.features.supports_foreign_keys:
+                    self.deferred_sql.append(
+                        self.sql_create_fk % {
+                            "name": self._create_index_name(model, [field.column], suffix="_fk_%s_%s" % (to_table, to_column)),
+                            "table": self.quote_name(model._meta.db_table),
+                            "column": self.quote_name(field.column),
+                            "to_table": self.quote_name(to_table),
+                            "to_column": self.quote_name(to_column),
+                        }
+                    )
+                elif self.sql_create_inline_fk:
+                    definition += " " + self.sql_create_inline_fk % {
                         "to_table": self.quote_name(to_table),
                         "to_column": self.quote_name(to_column),
                     }
-                )
+            # Add the SQL to our big list
+            column_sqls.append("%s %s" % (
+                self.quote_name(field.column),
+                definition,
+            ))
             # Autoincrement SQL (for backends with post table definition variant)
             if field.get_internal_type() == "AutoField":
                 autoinc_sql = self.connection.ops.autoinc_sql(model._meta.db_table, field.column)

+ 1 - 0
django/db/backends/sqlite3/schema.py

@@ -6,6 +6,7 @@ from django.db.models.fields.related import ManyToManyField
 class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
 
     sql_delete_table = "DROP TABLE %(table)s"
+    sql_create_inline_fk = "REFERENCES %(to_table)s (%(to_column)s)"
 
     def _remake_table(self, model, create_fields=[], delete_fields=[], alter_fields=[], rename_fields=[], override_uniques=None):
         """