We have script/generate migration add_fieldname_to_tablename fieldname:datatype
syntax for adding new columns to a model.
On the same line, do we have a script/generate for changing the datatype of a column? Or should I write SQL directly into my vanilla migration?
I want to change a column from datetime
to date
.
Use #change_column.
change_column(:table_name, :column_name, :date)
# a few more examples:
change_column(:suppliers, :name, :string, limit: 80)
change_column(:accounts, :description, :text)
NOTE: the same result can be achieved even outside of db migrations, this might be handy for testing/debugging but this method needs to be used very cautiously:
ActiveRecord::Base.connection.change_column(:table_name, :column_name, :date)