开发者

How to alter columns with ActiveRecord in Rails?

开发者 https://www.devze.com 2023-01-18 18:47 出处:网络
I\'m new to ActiveRecord. I realized that I had forgotten to add a default value for a column in one of my tables. I want to create a migration to fix this, but I can\'t figure out how. Is there an al

I'm new to ActiveRecord. I realized that I had forgotten to add a default value for a column in one of my tables. I want to create a migration to fix this, but I can't figure out how. Is there an alter_column method that you can call during migrations? If not, how can I do it?

EDIT: I just tried using change_column, but this causes an error like this:

-- change_column(:carts, :quantity, :integer, {:default=>1}) -> 0.0097s rake aborted! An error has occurred, this and all later migr开发者_高级运维ations canceled:

wrong number of arguments (0 for 1)


you can simply do a change_table:

change_table(:tablename) do |t|
   t.change :name, :string, :default => "something"
end

edit: in this case you can use change_column_default

0

精彩评论

暂无评论...
验证码 换一张
取 消