﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
37351	Altering the null attribute of a GeneratedField should raise when making migrations	Jacob Walls		"The migration layer attempts to detect if a `GeneratedField` has been altered in a SQL-affecting way so that it can throw an exception:

{{{#!py
        if modifying_generated_field:
            raise ValueError(
                f""Modifying GeneratedFields is not supported - the field {new_field} ""
                ""must be removed and re-added with the new definition.""
            )
}}}

However, altering the `null` kwarg, e.g. from `False` to `True` is not detected by the above check, but ''does'' have an incidence on the DDL that gets emitted, so a migration gets emitted that can do wacky things in the database, see ticket:37348#comment:6.

I propose to fix the `modifying_generated_field` logic so that it fathoms changes in the `null` attribute and raises.

This will be a breaking change but most likely a welcome one (removing dangerous operations from your migrations).

Rough test:
{{{#!diff
diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py
index 9bbf1249e9..907c255e22 100644
--- a/tests/migrations/test_operations.py
+++ b/tests/migrations/test_operations.py
@@ -6646,6 +6646,13 @@ class OperationTests(OperationTestBase):
             tests.append(
                 (""test_igfc_4"", generated_1, generated_3),
             )
+        generated_4 = models.GeneratedField(
+            expression=F(""pink"") + F(""pink"") + F(""pink""),
+            output_field=models.IntegerField(),
+            db_persist=db_persist,
+            null=True,
+        )
+        tests.append((""test_igfc_5"", generated_2, generated_4))
         for app_label, add_field, alter_field in tests:
             project_state = self.set_up_test_model(app_label)
             operations = [
}}}
{{{#!py
AssertionError: ValueError not raised
}}}"	Bug	new	Database layer (models, ORM)	6.1	Normal			Natalia Bidart	Unreviewed	0	0	0	0	0	0
