method

change_column

change_column(table_name, column_name, type, options = {})
public

Changes the column’s definition according to the new options. See TableDefinition#column for details of the options you can use.

Examples
 change_column(:suppliers, :name, :string, :limit => 80)
 change_column(:accounts, :description, :text)

7Notes

to set NULL => NO

ypetya · Jul 16, 20104 thanks

use :null => false

change_column :my_table, :my_column, :integer, :default => 0, :null => false

null to no effects

toddcesere · Oct 26, 20104 thanks

change_column will not query to replace the null values when you change null to false, even if you have a default set. This may cause the query to fail (may depend on the database used).

change_column_null will optionally take a value to replace nulls if you are setting null to false. If you want to set a default and disallow nulls you likely can't do both in one change_column call.

See Column definition

shark234 · Jan 11, 20102 thanks

Changing to MySql:BIGINT

ypetya · Jul 16, 20102 thanks

I can change a column type from INT to BIGINT with this command:

change_column :my_table, :my_column, :bigint

Will this method get rid of existing data?

tvle83 · Jan 14, 20101 thank

Will this method get rid of existing data?

Destroying Data

pgmcgee · Jul 8, 20101 thank

As far as I can tell, at least on a migration of a column from an integer to a decimal, this does not get rid of existing data.

On destroying data

foliosus · Aug 19, 2012

Reply to tvle83 and pgmcgee: the destructiveness of this method depends on your database. Some databases are better at converting between disparate types than others. For example, when changing a column from a numeric type to a string type, some databases drop the data where others will turn the numbers into their string representations.

Essentially, YMMV.