I completely understand that modifying a GeneratedField is either not possible, or comes with so many caveats that it’s not worth supporting in Django. However, Django will happily generate the AlterField migration and then error out trying to run it. Is it possible to refuse to generate the AlterField, either entirely or maybe just when the expression changes (allowing for renaming, for example)? Or even better, generate the migration as a RemoveField/AddField pair?
RemoveField/AddField pair seems like a pretty straight forward approach.
When altering a model’s existing generated field, you can run makemigrations. You can’t run migrate without getting the “ValueError: Modifying GeneratedFields not supported” error. The work around is to manually edit the generated migration file. Add migrations.RemoveField(), and then migrations.AddField() operations. The AlterField() parameters can simply be reused in the AddField() operation.
By definition a generated field can’t depend on another generated field. This means as long as the reference field doesn’t experience any data loss all the generated data can re-generate.