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.
This commit is contained in:
Ramiro Morales 2013-12-28 18:00:16 -03:00
parent 6d66ba5948
commit 5782c94f23
2 changed files with 20 additions and 12 deletions

View File

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

View File

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