summaryrefslogtreecommitdiff
path: root/tests/schema
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2024-04-16 00:11:36 -0400
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2024-04-16 17:27:38 +0200
commitd048f0d311cc9cb9578d687968f2d24a0a9efddb (patch)
treed18eca06a26cab47a3e8a2b345c6d02412951f4f /tests/schema
parent47c608202a58c8120d049c98d5d27c4609551d33 (diff)
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.
Diffstat (limited to 'tests/schema')
-rw-r--r--tests/schema/tests.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/tests/schema/tests.py b/tests/schema/tests.py
index ff126d446a..3a2947cf43 100644
--- a/tests/schema/tests.py
+++ b/tests/schema/tests.py
@@ -929,6 +929,39 @@ class SchemaTests(TransactionTestCase):
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):
name = CharField(max_length=255, primary_key=True)