Skip to content

Commit

Permalink
Added generation of SQLite FK DDL in initial migrations.
Browse files Browse the repository at this point in the history
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
ramiro committed Dec 28, 2013
1 parent 6d66ba5 commit 5782c94
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
31 changes: 19 additions & 12 deletions django/db/backends/schema.py
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions django/db/backends/sqlite3/schema.py
Expand Up @@ -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):
"""
Expand Down

0 comments on commit 5782c94

Please sign in to comment.