Refs #32779 -- Changed DatabaseSchemaEditor._unique_sql()/_create_unique_sql() to take fields as second parameter.
This commit is contained in:
parent
22da686ca9
commit
b746596f5f
|
@ -151,9 +151,9 @@ class BaseDatabaseSchemaEditor:
|
||||||
"""Take a model and return its table definition."""
|
"""Take a model and return its table definition."""
|
||||||
# Add any unique_togethers (always deferred, as some fields might be
|
# Add any unique_togethers (always deferred, as some fields might be
|
||||||
# created afterwards, like geometry fields with some backends).
|
# created afterwards, like geometry fields with some backends).
|
||||||
for fields in model._meta.unique_together:
|
for field_names in model._meta.unique_together:
|
||||||
columns = [model._meta.get_field(field).column for field in fields]
|
fields = [model._meta.get_field(field) for field in field_names]
|
||||||
self.deferred_sql.append(self._create_unique_sql(model, columns))
|
self.deferred_sql.append(self._create_unique_sql(model, fields))
|
||||||
# Create column SQL, add FK deferreds if needed.
|
# Create column SQL, add FK deferreds if needed.
|
||||||
column_sqls = []
|
column_sqls = []
|
||||||
params = []
|
params = []
|
||||||
|
@ -407,9 +407,9 @@ class BaseDatabaseSchemaEditor:
|
||||||
for fields in olds.difference(news):
|
for fields in olds.difference(news):
|
||||||
self._delete_composed_index(model, fields, {'unique': True}, self.sql_delete_unique)
|
self._delete_composed_index(model, fields, {'unique': True}, self.sql_delete_unique)
|
||||||
# Created uniques
|
# Created uniques
|
||||||
for fields in news.difference(olds):
|
for field_names in news.difference(olds):
|
||||||
columns = [model._meta.get_field(field).column for field in fields]
|
fields = [model._meta.get_field(field) for field in field_names]
|
||||||
self.execute(self._create_unique_sql(model, columns))
|
self.execute(self._create_unique_sql(model, fields))
|
||||||
|
|
||||||
def alter_index_together(self, model, old_index_together, new_index_together):
|
def alter_index_together(self, model, old_index_together, new_index_together):
|
||||||
"""
|
"""
|
||||||
|
@ -790,7 +790,7 @@ class BaseDatabaseSchemaEditor:
|
||||||
self._delete_primary_key(model, strict)
|
self._delete_primary_key(model, strict)
|
||||||
# Added a unique?
|
# Added a unique?
|
||||||
if self._unique_should_be_added(old_field, new_field):
|
if self._unique_should_be_added(old_field, new_field):
|
||||||
self.execute(self._create_unique_sql(model, [new_field.column]))
|
self.execute(self._create_unique_sql(model, [new_field]))
|
||||||
# Added an index? Add an index if db_index switched to True or a unique
|
# Added an index? Add an index if db_index switched to True or a unique
|
||||||
# constraint will no longer be used in lieu of an index. The following
|
# constraint will no longer be used in lieu of an index. The following
|
||||||
# lines from the truth table show all True cases; the rest are False:
|
# lines from the truth table show all True cases; the rest are False:
|
||||||
|
@ -1214,7 +1214,7 @@ class BaseDatabaseSchemaEditor:
|
||||||
self.deferred_sql.append(sql)
|
self.deferred_sql.append(sql)
|
||||||
return None
|
return None
|
||||||
constraint = self.sql_unique_constraint % {
|
constraint = self.sql_unique_constraint % {
|
||||||
'columns': ', '.join(map(self.quote_name, fields)),
|
'columns': ', '.join([self.quote_name(field.column) for field in fields]),
|
||||||
'deferrable': self._deferrable_constraint_sql(deferrable),
|
'deferrable': self._deferrable_constraint_sql(deferrable),
|
||||||
}
|
}
|
||||||
return self.sql_constraint % {
|
return self.sql_constraint % {
|
||||||
|
@ -1223,7 +1223,7 @@ class BaseDatabaseSchemaEditor:
|
||||||
}
|
}
|
||||||
|
|
||||||
def _create_unique_sql(
|
def _create_unique_sql(
|
||||||
self, model, columns, name=None, condition=None, deferrable=None,
|
self, model, fields, name=None, condition=None, deferrable=None,
|
||||||
include=None, opclasses=None, expressions=None,
|
include=None, opclasses=None, expressions=None,
|
||||||
):
|
):
|
||||||
if (
|
if (
|
||||||
|
@ -1242,6 +1242,7 @@ class BaseDatabaseSchemaEditor:
|
||||||
|
|
||||||
compiler = Query(model, alias_cols=False).get_compiler(connection=self.connection)
|
compiler = Query(model, alias_cols=False).get_compiler(connection=self.connection)
|
||||||
table = model._meta.db_table
|
table = model._meta.db_table
|
||||||
|
columns = [field.column for field in fields]
|
||||||
if name is None:
|
if name is None:
|
||||||
name = IndexName(table, columns, '_uniq', create_unique_name)
|
name = IndexName(table, columns, '_uniq', create_unique_name)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -187,7 +187,7 @@ class UniqueConstraint(BaseConstraint):
|
||||||
)
|
)
|
||||||
|
|
||||||
def constraint_sql(self, model, schema_editor):
|
def constraint_sql(self, model, schema_editor):
|
||||||
fields = [model._meta.get_field(field_name).column for field_name in self.fields]
|
fields = [model._meta.get_field(field_name) for field_name in self.fields]
|
||||||
include = [model._meta.get_field(field_name).column for field_name in self.include]
|
include = [model._meta.get_field(field_name).column for field_name in self.include]
|
||||||
condition = self._get_condition_sql(model, schema_editor)
|
condition = self._get_condition_sql(model, schema_editor)
|
||||||
expressions = self._get_index_expressions(model, schema_editor)
|
expressions = self._get_index_expressions(model, schema_editor)
|
||||||
|
@ -198,7 +198,7 @@ class UniqueConstraint(BaseConstraint):
|
||||||
)
|
)
|
||||||
|
|
||||||
def create_sql(self, model, schema_editor):
|
def create_sql(self, model, schema_editor):
|
||||||
fields = [model._meta.get_field(field_name).column for field_name in self.fields]
|
fields = [model._meta.get_field(field_name) for field_name in self.fields]
|
||||||
include = [model._meta.get_field(field_name).column for field_name in self.include]
|
include = [model._meta.get_field(field_name).column for field_name in self.include]
|
||||||
condition = self._get_condition_sql(model, schema_editor)
|
condition = self._get_condition_sql(model, schema_editor)
|
||||||
expressions = self._get_index_expressions(model, schema_editor)
|
expressions = self._get_index_expressions(model, schema_editor)
|
||||||
|
|
|
@ -321,6 +321,9 @@ backends.
|
||||||
``iso_year`` argument in order to support bounds for ISO-8601 week-numbering
|
``iso_year`` argument in order to support bounds for ISO-8601 week-numbering
|
||||||
years.
|
years.
|
||||||
|
|
||||||
|
* The second argument of ``DatabaseSchemaEditor._unique_sql()`` and
|
||||||
|
``_create_unique_sql()`` methods is now fields instead of columns.
|
||||||
|
|
||||||
:mod:`django.contrib.gis`
|
:mod:`django.contrib.gis`
|
||||||
-------------------------
|
-------------------------
|
||||||
|
|
||||||
|
|
|
@ -3287,7 +3287,7 @@ class SchemaTests(TransactionTestCase):
|
||||||
|
|
||||||
constraint_name = 'CamelCaseUniqConstraint'
|
constraint_name = 'CamelCaseUniqConstraint'
|
||||||
expected_constraint_name = identifier_converter(constraint_name)
|
expected_constraint_name = identifier_converter(constraint_name)
|
||||||
editor.execute(editor._create_unique_sql(model, [field.column], constraint_name))
|
editor.execute(editor._create_unique_sql(model, [field], constraint_name))
|
||||||
self.assertIn(expected_constraint_name, self.get_constraints(model._meta.db_table))
|
self.assertIn(expected_constraint_name, self.get_constraints(model._meta.db_table))
|
||||||
editor.alter_field(model, get_field(unique=True), field, strict=True)
|
editor.alter_field(model, get_field(unique=True), field, strict=True)
|
||||||
self.assertNotIn(expected_constraint_name, self.get_constraints(model._meta.db_table))
|
self.assertNotIn(expected_constraint_name, self.get_constraints(model._meta.db_table))
|
||||||
|
|
Loading…
Reference in New Issue