1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

Fixed #35373 -- Fixed a crash when indexing a generated field on SQLite.

Generated fields have to be excluded from the INSERT query against the remade
table including the index.

Thanks Moshe Dicker for the report, David Sanders and Mariusz Felisiak for the
review.
This commit is contained in:
Simon Charette
2024-04-16 00:11:36 -04:00
committed by Sarah Boyce
parent 47c608202a
commit d048f0d311
3 changed files with 40 additions and 1 deletions

View File

@@ -928,6 +928,39 @@ class SchemaTests(TransactionTestCase):
self.assertEqual(obj.generated, "foo%")
self.assertIs(obj.contains_foo, True)
@isolate_apps("schema")
@skipUnlessDBFeature("supports_stored_generated_columns")
def test_alter_generated_field(self):
class GeneratedFieldIndexedModel(Model):
number = IntegerField(default=1)
generated = GeneratedField(
expression=F("number"),
db_persist=True,
output_field=IntegerField(),
)
class Meta:
app_label = "schema"
with connection.schema_editor() as editor:
editor.create_model(GeneratedFieldIndexedModel)
old_field = GeneratedFieldIndexedModel._meta.get_field("generated")
new_field = GeneratedField(
expression=F("number"),
db_persist=True,
db_index=True,
output_field=IntegerField(),
)
new_field.contribute_to_class(GeneratedFieldIndexedModel, "generated")
with connection.schema_editor() as editor:
editor.alter_field(GeneratedFieldIndexedModel, old_field, new_field)
self.assertIn(
"generated", self.get_indexes(GeneratedFieldIndexedModel._meta.db_table)
)
@isolate_apps("schema")
def test_add_auto_field(self):
class AddAutoFieldModel(Model):