Flowdock
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)
Show source
Register or log in to add new notes.
July 16, 2010
3 thanks

to set NULL => NO

use :null => false

change_column :my_table, :my_column, :integer, :default => 0, :null => false
October 26, 2010
1 thank

null to no effects

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.

January 11, 2010
1 thank
August 19, 2012
0 thanks

On destroying data

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.

July 16, 2010
0 thanks

Changing to MySql:BIGINT

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

change_column :my_table, :my_column, :bigint
January 14, 2010
0 thanks

Will this method get rid of existing data?

Will this method get rid of existing data?

July 8, 2010
0 thanks

Destroying Data

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.