1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #29868 -- Retained database constraints on SQLite table rebuilds.

Refs #11964.

Thanks Scott Stevens for testing this upcoming feature and the report.
This commit is contained in:
Simon Charette
2018-10-29 05:33:41 -04:00
committed by Carlton Gibson
parent f77fc56c96
commit 95bda03f2d
3 changed files with 50 additions and 27 deletions

View File

@@ -126,8 +126,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
else:
super().alter_field(model, old_field, new_field, strict=strict)
def _remake_table(self, model, create_field=None, delete_field=None, alter_field=None,
add_constraint=None, remove_constraint=None):
def _remake_table(self, model, create_field=None, delete_field=None, alter_field=None):
"""
Shortcut to transform a model from old_model into new_model
@@ -224,13 +223,6 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
]
constraints = list(model._meta.constraints)
if add_constraint:
constraints.append(add_constraint)
if remove_constraint:
constraints = [
constraint for constraint in constraints
if remove_constraint.name != constraint.name
]
# Construct a new model for the new state
meta_contents = {
@@ -383,7 +375,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
self.delete_model(old_field.remote_field.through)
def add_constraint(self, model, constraint):
self._remake_table(model, add_constraint=constraint)
self._remake_table(model)
def remove_constraint(self, model, constraint):
self._remake_table(model, remove_constraint=constraint)
self._remake_table(model)

View File

@@ -819,6 +819,7 @@ class AddConstraint(IndexOperation):
def state_forwards(self, app_label, state):
model_state = state.models[app_label, self.model_name_lower]
model_state.options[self.option_name] = [*model_state.options[self.option_name], self.constraint]
state.reload_model(app_label, self.model_name_lower, delay=True)
def database_forwards(self, app_label, schema_editor, from_state, to_state):
model = to_state.apps.get_model(app_label, self.model_name)
@@ -851,9 +852,10 @@ class RemoveConstraint(IndexOperation):
model_state = state.models[app_label, self.model_name_lower]
constraints = model_state.options[self.option_name]
model_state.options[self.option_name] = [c for c in constraints if c.name != self.name]
state.reload_model(app_label, self.model_name_lower, delay=True)
def database_forwards(self, app_label, schema_editor, from_state, to_state):
model = from_state.apps.get_model(app_label, self.model_name)
model = to_state.apps.get_model(app_label, self.model_name)
if self.allow_migrate_model(schema_editor.connection.alias, model):
from_model_state = from_state.models[app_label, self.model_name_lower]
constraint = from_model_state.get_constraint_by_name(self.name)