How do I migrate a database column from integer to bigint in rails?
I just ran into the same issue. The following worked for me:
def up
change_column :my_table, :my_column, :bigint
end
For anybody looking for different data types than :bigint
, you can use :tinyint
, :smallint
and :mediumint
as well, according to ActiveRecord type of integer (tinyint, smallint, mediumint, int, bigint)
# activerecord-3.0.0/lib/active_record/connection_adapters/mysql_adapter.rb
# Maps logical Rails types to MySQL-specific data types.
def type_to_sql(type, limit = nil, precision = nil, scale = nil)
return super unless type.to_s == 'integer'
case limit
when 1; 'tinyint'
when 2; 'smallint'
when 3; 'mediumint'
when nil, 4, 11; 'int(11)' # compatibility with MySQL default
when 5..8; 'bigint'
else raise(ActiveRecordError, "No integer type has byte size #{limit}")
end
end
Also, do use up
and down
for rails db:rollback
; this is what worked for me:
class ChangeCarNumberOfKeysToSmallInt < ActiveRecord::Migration[5.2]
def up
change_column :cars, :number_of_keys, :tinyint
end
def down
change_column :cars, :number_of_keys, :int
end
end