I wrongly named a column hased_password
instead of hashed_password
.
How do I update the database schema, using migration to rename this column?
alter-tablemigrationrenameruby-on-railsruby-on-rails-3
I wrongly named a column hased_password
instead of hashed_password
.
How do I update the database schema, using migration to rename this column?
Best Answer
You'll probably want to create a separate migration to do this. (Rename
FixColumnName
as you will.):Then edit the migration to do your will:
For Rails 3.1 use:
While, the
up
anddown
methods still apply, Rails 3.1 receives achange
method that "knows how to migrate your database and reverse it when the migration is rolled back without the need to write a separate down method".See "Active Record Migrations" for more information.
If you happen to have a whole bunch of columns to rename, or something that would have required repeating the table name over and over again:
You could use
change_table
to keep things a little neater:Then just
db:migrate
as usual or however you go about your business.For Rails 4:
While creating a
Migration
for renaming a column, Rails 4 generates achange
method instead ofup
anddown
as mentioned in the above section. The generatedchange
method is:which will create a migration file similar to: