#35969: Changing output_field for GeneratedField leads to ProgrammingError with
Postgres 16.5+
-----------------------------+------------------------------------
Reporter: Ryan Schave | Owner: (none)
Type: Bug | Status: new
Component: Migrations | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+------------------------------------
Changes (by Sarah Boyce):
* cc: Lily Foote, Mariusz Felisiak (added)
* component: Uncategorized => Migrations
* stage: Unreviewed => Accepted
* type: Uncategorized => Bug
Comment:
Thank you for the detailed report
Replicated the error
{{{#!diff
diff --git a/tests/migrations/test_operations.py
b/tests/migrations/test_operations.py
index 6312a7d4a2..1b75c609b3 100644
--- a/tests/migrations/test_operations.py
+++ b/tests/migrations/test_operations.py
@@ -6185,6 +6185,38 @@ class OperationTests(OperationTestBase):
with self.assertRaisesMessage(ValueError, msg):
self.apply_operations(app_label, project_state, operations)
+ @skipUnlessDBFeature("supports_stored_generated_columns")
+ def test_generated_field_changes_output_field(self):
+ app_label = "test_gfcof"
+ operation = migrations.AddField(
+ "Pony",
+ "modified_pink",
+ models.GeneratedField(
+ expression=F("pink") + F("pink"),
+ output_field=models.IntegerField(),
+ db_persist=True,
+ ),
+ )
+ project_state, new_state = self.make_test_state(app_label,
operation)
+ # Add generated column.
+ with connection.schema_editor() as editor:
+ operation.database_forwards(app_label, editor, project_state,
new_state)
+ # Update output_field used in the generated field.
+ operations = [
+ migrations.AlterField(
+ "Pony",
+ "modified_pink",
+ models.GeneratedField(
+ expression=F("pink") + F("pink"),
+ output_field=models.DecimalField(decimal_places=2,
max_digits=16),
+ db_persist=True,
+ ),
+ ),
+ ]
+ new_state = self.apply_operations(app_label, new_state,
operations)
+ with connection.schema_editor() as editor:
+ operation.database_forwards(app_label, editor, project_state,
new_state)
+
def _test_add_generated_field(self, db_persist):
app_label = "test_agf"
}}}
> {{{
> ValueError: Modifying GeneratedFields is not supported - the field
sales.Order.total_cost must be removed and re-added with the new
definition.
> }}}
>
> This error is more helpful. It explains the problem better and even
suggests a workaround.
Having this error instead makes sense to me.
CC-ed a couple of other folks who might have thoughts
--
Ticket URL: <https://code.djangoproject.com/ticket/35969#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/django-updates/0107019390edefd2-741b1a6d-7912-4a34-a3cc-22625224c085-000000%40eu-central-1.amazonses.com.