1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

[2.2.x] Refs #30172 -- Prevented removing a field's check or unique constraint from removing Meta constraints.

Backport of 4bb859e246 from master.
This commit is contained in:
Paveł Tyślacki
2019-02-11 17:17:06 +03:00
committed by Tim Graham
parent 40b0a58f5f
commit 3dd5e71752
5 changed files with 125 additions and 8 deletions

View File

@@ -281,6 +281,10 @@ class BaseDatabaseFeatures:
supports_partial_indexes = True
supports_functions_in_partial_indexes = True
# Does the database allow more than one constraint or index on the same
# field(s)?
allows_multiple_constraints_on_same_fields = True
def __init__(self, connection):
self.connection = connection

View File

@@ -548,7 +548,11 @@ class BaseDatabaseSchemaEditor:
# Has unique been removed?
if old_field.unique and (not new_field.unique or self._field_became_primary_key(old_field, new_field)):
# Find the unique constraint for this field
constraint_names = self._constraint_names(model, [old_field.column], unique=True, primary_key=False)
meta_constraint_names = {constraint.name for constraint in model._meta.constraints}
constraint_names = self._constraint_names(
model, [old_field.column], unique=True, primary_key=False,
exclude=meta_constraint_names,
)
if strict and len(constraint_names) != 1:
raise ValueError("Found wrong number (%s) of unique constraints for %s.%s" % (
len(constraint_names),
@@ -598,7 +602,11 @@ class BaseDatabaseSchemaEditor:
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']:
constraint_names = self._constraint_names(model, [old_field.column], check=True)
meta_constraint_names = {constraint.name for constraint in model._meta.constraints}
constraint_names = self._constraint_names(
model, [old_field.column], check=True,
exclude=meta_constraint_names,
)
if strict and len(constraint_names) != 1:
raise ValueError("Found wrong number (%s) of check constraints for %s.%s" % (
len(constraint_names),
@@ -1089,7 +1097,7 @@ class BaseDatabaseSchemaEditor:
def _constraint_names(self, model, column_names=None, unique=None,
primary_key=None, index=None, foreign_key=None,
check=None, type_=None):
check=None, type_=None, exclude=None):
"""Return all constraint names matching the columns and conditions."""
if column_names is not None:
column_names = [
@@ -1113,7 +1121,8 @@ class BaseDatabaseSchemaEditor:
continue
if type_ is not None and infodict['type'] != type_:
continue
result.append(name)
if not exclude or name not in exclude:
result.append(name)
return result
def _delete_primary_key(self, model, strict=False):

View File

@@ -56,6 +56,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
supports_ignore_conflicts = False
max_query_params = 2**16 - 1
supports_partial_indexes = False
allows_multiple_constraints_on_same_fields = False
@cached_property
def has_fetch_offset_support(self):