Rails migration for change column Rails migration for change column ruby-on-rails ruby-on-rails

Rails migration for change column


I think this should work.

change_column :table_name, :column_name, :date


You can also use a block if you have multiple columns to change within a table.

Example:

change_table :table_name do |t|  t.change :column_name, :column_type, {options}end

See the API documentation on the Table class for more details.


I'm not aware if you can create a migration from the command line to do all this, but you can create a new migration, then edit the migration to perform this taks.

If tablename is the name of your table, fieldname is the name of your field and you want to change from a datetime to date, you can write a migration to do this.

You can create a new migration with:

rails g migration change_data_type_for_fieldname

Then edit the migration to use change_table:

class ChangeDataTypeForFieldname < ActiveRecord::Migration  def self.up    change_table :tablename do |t|      t.change :fieldname, :date    end  end  def self.down    change_table :tablename do |t|      t.change :fieldname, :datetime    end  endend

Then run the migration:

rake db:migrate