If I used the generator for a table (and controller/view code) , how do I use the generator to add a single field retroactively?

I used the generator to create a table and respective controller and view data using this command:

mix phx.gen.html TestBeds TestBed testbeds name:string version:string note:string developer:string status:string status_action_value:string

I want to add a field to this using the generator so I don’t need to manually update each code piece throughout the codebase. How do I do that?

Also, if I want to remove a field using the generator, how can I do that?

Thank you.

Updating existing code is not supported by the generators.

To add a little to @LostKobrakai’s reponse… let’s consider two cases:

  1. The generated code is not yet committed / deployed - you’ve just run the generator, played with the application and noticed you’d like to change something

  2. The code is already committed / deployed, so it’s likely that somebody else relies on the current state of it (your app’s users, colleagues…)

For 1), you could always roll back the changes, remove the generated files and re-run the generator, adding / removing fields as needed, that’s easy.

For 2), it’s of course more complicated. For instance, you should not modify the existing migration, instead creating a new one for altering the table. You also may have made other changes to the migration, schema module and its changeset function(s), controller, view markup, etc. Because of this, it’s quite difficult to modify all the bits and pieces via a generator in a way that is compatible with the existing code, so in this case, it’s (best) left to you to make the changes manually. That’s not to say it would be impossible, but last I checked the phx.gen.* tasks mostly work by writing files based off of templates, not by reading / modifying the existing code (exception being the additions to the context module if one already exists, but that is also done by simply injecting code at the end of the file.)