From 7ef2398e814e044d54b68359d473cc64902a8c31 Mon Sep 17 00:00:00 2001 From: Hannes Ljungberg Date: Sun, 23 May 2021 14:25:54 +0200 Subject: [PATCH] Fixed #32777 -- Passed table reference as a string to DatabaseSchemaEditor._index_columns(). --- django/db/backends/base/schema.py | 8 ++++---- tests/schema/tests.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py index ad2f5a7da1..5cecbb7d60 100644 --- a/django/db/backends/base/schema.py +++ b/django/db/backends/base/schema.py @@ -1241,9 +1241,9 @@ class BaseDatabaseSchemaEditor: return self.quote_name(self._create_index_name(*args, **kwargs)) compiler = Query(model, alias_cols=False).get_compiler(connection=self.connection) - table = Table(model._meta.db_table, self.quote_name) + table = model._meta.db_table if name is None: - name = IndexName(model._meta.db_table, columns, '_uniq', create_unique_name) + name = IndexName(table, columns, '_uniq', create_unique_name) else: name = self.quote_name(name) if condition or include or opclasses or expressions: @@ -1253,10 +1253,10 @@ class BaseDatabaseSchemaEditor: if columns: columns = self._index_columns(table, columns, col_suffixes=(), opclasses=opclasses) else: - columns = Expressions(model._meta.db_table, expressions, compiler, self.quote_value) + columns = Expressions(table, expressions, compiler, self.quote_value) return Statement( sql, - table=table, + table=Table(table, self.quote_name), name=name, columns=columns, condition=self._index_condition_sql(condition), diff --git a/tests/schema/tests.py b/tests/schema/tests.py index c5f5bd2e85..036ac29dce 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -2198,6 +2198,22 @@ class SchemaTests(TransactionTestCase): AuthorWithUniqueNameAndBirthday._meta.constraints = [] editor.remove_constraint(AuthorWithUniqueNameAndBirthday, constraint) + def test_unique_constraint(self): + with connection.schema_editor() as editor: + editor.create_model(Author) + constraint = UniqueConstraint(fields=['name'], name='name_uq') + # Add constraint. + with connection.schema_editor() as editor: + editor.add_constraint(Author, constraint) + sql = constraint.create_sql(Author, editor) + table = Author._meta.db_table + self.assertIs(sql.references_table(table), True) + self.assertIs(sql.references_column(table, 'name'), True) + # Remove constraint. + with connection.schema_editor() as editor: + editor.remove_constraint(Author, constraint) + self.assertNotIn(constraint.name, self.get_constraints(table)) + @skipUnlessDBFeature('supports_expression_indexes') def test_func_unique_constraint(self): with connection.schema_editor() as editor: