altering GeneratedFields

Before re-opening #35061 (Modifying GeneratedFields is not supported - not documented under model field reference for GeneratedField) – Django or submitting a new ticket, I was hoping to get some clarification here first.

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?

1 Like

I don’t have any insight, but was just wondering the same thing, as I finally got to use a GeneratedField myself. Thank you for starting this topic!

I think if we don’t support AlterField, then this would be a better developer experience.

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.