Alex Rivera | Logout

Add a default value to a column through a migration

Asked 2011-08-17T19:53:08.003
323

How do I add a default value to a column that already exists through a migration?

All the documentation I can find shows you how to do it if the column doesn't already exist but in this case it does.

Edit
Report

1 Answer

11

Execute:

rails generate migration add_column_to_table column:boolean

It will generate this migration:

class AddColumnToTable < ActiveRecord::Migration
  def change
    add_column :table, :column, :boolean
  end
end

Set the default value adding :default => 1

add_column :table, :column, :boolean, :default => 1

Run:

rake db:migrate

answered 2013-01-20T15:09:41.477

Your Answer