Saturday, August 13, 2016

How to rename a database column in Rails migration?

If you have not deployed the code to any environment, you can roll back the migration.

rake db:rollback

And create the migration with the right column by correcting the column name. If you have lot of data that you don't want to lose then renaming is the way to go.

Generate a new migration file for renaming the old column:

rails g migration RenameOldColumnToNewColumn
Then modify the change method in the generated migration file:

rename_column :table_name, :old_column, :new_column

No comments:

Post a Comment