diff options
| author | Paveł Tyślacki <pavel.tyslacki@gmail.com> | 2019-02-11 17:24:10 +0300 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2019-03-17 21:28:49 -0400 |
| commit | 2a92e2e3c12e5c4cad0ce2a2d5964675ef837fe7 (patch) | |
| tree | c3fa67d8370a47a2cbb89544fde876365fe32ec2 /django/db | |
| parent | 3dd5e71752c993df00e01fca8086a1af5ba89176 (diff) | |
[2.2.x] Refs #30172 -- Prevented removing a model Meta's index/unique_together from removing Meta constraints/indexes.
Backport of 5c17c273ae2d7274f1fa78218b3b74690efddb86 from master.
Diffstat (limited to 'django/db')
| -rw-r--r-- | django/db/backends/base/schema.py | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py index 19c9a04ba8..3c80dd48ff 100644 --- a/django/db/backends/base/schema.py +++ b/django/db/backends/base/schema.py @@ -383,8 +383,13 @@ class BaseDatabaseSchemaEditor: self.execute(self._create_index_sql(model, fields, suffix="_idx")) def _delete_composed_index(self, model, fields, constraint_kwargs, sql): + meta_constraint_names = {constraint.name for constraint in model._meta.constraints} + meta_index_names = {constraint.name for constraint in model._meta.indexes} columns = [model._meta.get_field(field).column for field in fields] - constraint_names = self._constraint_names(model, columns, **constraint_kwargs) + constraint_names = self._constraint_names( + model, columns, exclude=meta_constraint_names | meta_index_names, + **constraint_kwargs + ) if len(constraint_names) != 1: raise ValueError("Found wrong number (%s) of constraints for %s(%s)" % ( len(constraint_names), @@ -593,13 +598,15 @@ class BaseDatabaseSchemaEditor: meta_index_names = {index.name for index in model._meta.indexes} # Retrieve only BTREE indexes since this is what's created with # db_index=True. - index_names = self._constraint_names(model, [old_field.column], index=True, type_=Index.suffix) + index_names = self._constraint_names( + model, [old_field.column], index=True, type_=Index.suffix, + exclude=meta_index_names, + ) for index_name in index_names: - if index_name not in meta_index_names: - # The only way to check if an index was created with - # db_index=True or with Index(['field'], name='foo') - # is to look at its name (refs #28053). - self.execute(self._delete_index_sql(model, index_name)) + # The only way to check if an index was created with + # db_index=True or with Index(['field'], name='foo') + # is to look at its name (refs #28053). + self.execute(self._delete_index_sql(model, index_name)) # Change check constraints? if old_db_params['check'] != new_db_params['check'] and old_db_params['check']: meta_constraint_names = {constraint.name for constraint in model._meta.constraints} |
