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)
to set NULL => NO
use :null => false
change_column :my_table, :my_column, :integer, :default => 0, :null => false
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.
Changing to MySql:BIGINT
I can change a column type from INT to BIGINT with this command:
change_column :my_table, :my_column, :bigint
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.
Will this method get rid of existing data?
Will this method get rid of existing data?
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.