Alex Rivera | Logout

How can I rename a database column in a Ruby on Rails migration?

Asked 2010-01-02T16:18:42.873
1586

I wrongly named a column hased_password instead of hashed_password.

How do I update the database schema, using migration to rename this column?

Edit
Report

2 Answers

73

In my opinion, in this case, it's better to use rake db:rollback, then edit your migration and again run rake db:migrate.

However, if you have data in the column you don't want to lose, then use rename_column.

answered 2010-01-03T00:55:51.610
9

As an alternative option, if you are not married to the idea of migrations, there is a compelling gem for ActiveRecord which will handle the name changes automatically for you, Datamapper style. All you do is change the column name in your model, and make sure you put Model.auto_upgrade! at the bottom of your model.rb, and viola! The database is updated on the fly.

See https://github.com/DAddYE/mini_record

Note: You will need to nuke db/schema.rb to prevent conflicts.

It is still in the beta phase and obviously not for everyone, but it is still a compelling choice. I am currently using it in two non-trivial production apps with no issues.

answered 2012-01-30T20:00:58.940

Your Answer