Fixed #32777 -- Passed table reference as a string to DatabaseSchemaEditor._index_columns().

This commit is contained in:
Hannes Ljungberg 2021-05-23 14:25:54 +02:00 committed by Mariusz Felisiak
parent 5e04e84d67
commit 7ef2398e81
2 changed files with 20 additions and 4 deletions

View File

@ -1241,9 +1241,9 @@ class BaseDatabaseSchemaEditor:
return self.quote_name(self._create_index_name(*args, **kwargs)) return self.quote_name(self._create_index_name(*args, **kwargs))
compiler = Query(model, alias_cols=False).get_compiler(connection=self.connection) 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: if name is None:
name = IndexName(model._meta.db_table, columns, '_uniq', create_unique_name) name = IndexName(table, columns, '_uniq', create_unique_name)
else: else:
name = self.quote_name(name) name = self.quote_name(name)
if condition or include or opclasses or expressions: if condition or include or opclasses or expressions:
@ -1253,10 +1253,10 @@ class BaseDatabaseSchemaEditor:
if columns: if columns:
columns = self._index_columns(table, columns, col_suffixes=(), opclasses=opclasses) columns = self._index_columns(table, columns, col_suffixes=(), opclasses=opclasses)
else: else:
columns = Expressions(model._meta.db_table, expressions, compiler, self.quote_value) columns = Expressions(table, expressions, compiler, self.quote_value)
return Statement( return Statement(
sql, sql,
table=table, table=Table(table, self.quote_name),
name=name, name=name,
columns=columns, columns=columns,
condition=self._index_condition_sql(condition), condition=self._index_condition_sql(condition),

View File

@ -2198,6 +2198,22 @@ class SchemaTests(TransactionTestCase):
AuthorWithUniqueNameAndBirthday._meta.constraints = [] AuthorWithUniqueNameAndBirthday._meta.constraints = []
editor.remove_constraint(AuthorWithUniqueNameAndBirthday, constraint) 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') @skipUnlessDBFeature('supports_expression_indexes')
def test_func_unique_constraint(self): def test_func_unique_constraint(self):
with connection.schema_editor() as editor: with connection.schema_editor() as editor: