Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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.
  • Loading branch information...
commit 5782c94f2382303dd28a35a9b9591ad480286220 1 parent 6d66ba5
Ramiro Morales ramiro authored
Showing with 20 additions and 12 deletions.
  1. +19 −12 django/db/backends/schema.py
  2. +1 −0  django/db/backends/sqlite3/schema.py
31 django/db/backends/schema.py
View
@@ -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 @@ def create_model(self, model):
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 @@ def create_model(self, model):
}
)
# 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  django/db/backends/sqlite3/schema.py
View
@@ -6,6 +6,7 @@
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):
"""
Please sign in to comment.
Something went wrong with that request. Please try again.