1
0
mirror of https://github.com/django/django.git synced 2025-10-29 00:26:07 +00:00

[4.0.x] Fixed #33194 -- Fixed migrations when altering a field with functional indexes/unique constraints on SQLite.

This adjusts Expressions.rename_table_references() to only update alias
when needed.

Regression in 83fcfc9ec8.

Co-authored-by: Simon Charette <charettes@users.noreply.github.com>

Backport of 86971c4090 from main
This commit is contained in:
Hannes Ljungberg
2021-10-15 22:17:26 +02:00
committed by Mariusz Felisiak
parent f5fd03aebe
commit 00aa3e0b9b
4 changed files with 64 additions and 6 deletions

View File

@@ -2106,6 +2106,25 @@ class OperationTests(OperationTestBase):
self.assertEqual(definition[1], [])
self.assertEqual(definition[2], {'model_name': 'Pony', 'name': index_name})
@skipUnlessDBFeature('supports_expression_indexes')
def test_alter_field_with_func_index(self):
app_label = 'test_alfuncin'
index_name = f'{app_label}_pony_idx'
table_name = f'{app_label}_pony'
project_state = self.set_up_test_model(
app_label,
indexes=[models.Index(Abs('pink'), name=index_name)],
)
operation = migrations.AlterField('Pony', 'pink', models.IntegerField(null=True))
new_state = project_state.clone()
operation.state_forwards(app_label, new_state)
with connection.schema_editor() as editor:
operation.database_forwards(app_label, editor, project_state, new_state)
self.assertIndexNameExists(table_name, index_name)
with connection.schema_editor() as editor:
operation.database_backwards(app_label, editor, new_state, project_state)
self.assertIndexNameExists(table_name, index_name)
def test_alter_field_with_index(self):
"""
Test AlterField operation with an index to ensure indexes created via
@@ -2664,6 +2683,26 @@ class OperationTests(OperationTestBase):
'name': 'covering_pink_constraint_rm',
})
def test_alter_field_with_func_unique_constraint(self):
app_label = 'test_alfuncuc'
constraint_name = f'{app_label}_pony_uq'
table_name = f'{app_label}_pony'
project_state = self.set_up_test_model(
app_label,
constraints=[models.UniqueConstraint('pink', 'weight', name=constraint_name)]
)
operation = migrations.AlterField('Pony', 'pink', models.IntegerField(null=True))
new_state = project_state.clone()
operation.state_forwards(app_label, new_state)
with connection.schema_editor() as editor:
operation.database_forwards(app_label, editor, project_state, new_state)
if connection.features.supports_expression_indexes:
self.assertIndexNameExists(table_name, constraint_name)
with connection.schema_editor() as editor:
operation.database_backwards(app_label, editor, new_state, project_state)
if connection.features.supports_expression_indexes:
self.assertIndexNameExists(table_name, constraint_name)
def test_add_func_unique_constraint(self):
app_label = 'test_adfuncuc'
constraint_name = f'{app_label}_pony_abs_uq'