Change an existing column's type
suggest changeCreate a migration, then call change_column yourself. A name like change_column_in_table does not make the generator write that call:
rails g migration change_column_in_table
The new file is under db/migrate/, with a UTC timestamp in the name. Fill in change:
def change
change_column :table_name, :column_name, :new_type
end
Rails Guide – Changing Columns
A longer but safer method
change_column inside change cannot be reversed automatically. Split it into up and down so rollback can restore the old type:
def up
change_column :my_table, :my_column, :new_type
end
def down
change_column :my_table, :my_column, :old_type
end
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents