diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py index 5cecbb7d60..c409464eca 100644 --- a/django/db/backends/base/schema.py +++ b/django/db/backends/base/schema.py @@ -151,9 +151,9 @@ class BaseDatabaseSchemaEditor: """Take a model and return its table definition.""" # Add any unique_togethers (always deferred, as some fields might be # created afterwards, like geometry fields with some backends). - for fields in model._meta.unique_together: - columns = [model._meta.get_field(field).column for field in fields] - self.deferred_sql.append(self._create_unique_sql(model, columns)) + for field_names in model._meta.unique_together: + fields = [model._meta.get_field(field) for field in field_names] + self.deferred_sql.append(self._create_unique_sql(model, fields)) # Create column SQL, add FK deferreds if needed. column_sqls = [] params = [] @@ -407,9 +407,9 @@ class BaseDatabaseSchemaEditor: for fields in olds.difference(news): self._delete_composed_index(model, fields, {'unique': True}, self.sql_delete_unique) # Created uniques - for fields in news.difference(olds): - columns = [model._meta.get_field(field).column for field in fields] - self.execute(self._create_unique_sql(model, columns)) + for field_names in news.difference(olds): + fields = [model._meta.get_field(field) for field in field_names] + self.execute(self._create_unique_sql(model, fields)) def alter_index_together(self, model, old_index_together, new_index_together): """ @@ -790,7 +790,7 @@ class BaseDatabaseSchemaEditor: self._delete_primary_key(model, strict) # Added a unique? 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 # 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: @@ -1214,7 +1214,7 @@ class BaseDatabaseSchemaEditor: self.deferred_sql.append(sql) return None 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), } return self.sql_constraint % { @@ -1223,7 +1223,7 @@ class BaseDatabaseSchemaEditor: } 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, ): if ( @@ -1242,6 +1242,7 @@ class BaseDatabaseSchemaEditor: compiler = Query(model, alias_cols=False).get_compiler(connection=self.connection) table = model._meta.db_table + columns = [field.column for field in fields] if name is None: name = IndexName(table, columns, '_uniq', create_unique_name) else: diff --git a/django/db/models/constraints.py b/django/db/models/constraints.py index 6dfc42942f..8ae05a504c 100644 --- a/django/db/models/constraints.py +++ b/django/db/models/constraints.py @@ -187,7 +187,7 @@ class UniqueConstraint(BaseConstraint): ) 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] condition = self._get_condition_sql(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): - 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] condition = self._get_condition_sql(model, schema_editor) expressions = self._get_index_expressions(model, schema_editor) diff --git a/docs/releases/4.0.txt b/docs/releases/4.0.txt index aa82afa4a5..831bb9b854 100644 --- a/docs/releases/4.0.txt +++ b/docs/releases/4.0.txt @@ -321,6 +321,9 @@ backends. ``iso_year`` argument in order to support bounds for ISO-8601 week-numbering years. +* The second argument of ``DatabaseSchemaEditor._unique_sql()`` and + ``_create_unique_sql()`` methods is now fields instead of columns. + :mod:`django.contrib.gis` ------------------------- diff --git a/tests/schema/tests.py b/tests/schema/tests.py index 036ac29dce..eb9be178db 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -3287,7 +3287,7 @@ class SchemaTests(TransactionTestCase): constraint_name = 'CamelCaseUniqConstraint' 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)) editor.alter_field(model, get_field(unique=True), field, strict=True) self.assertNotIn(expected_constraint_name, self.get_constraints(model._meta.db_table))