Rails数据库添加列

13
在Rails中创建模型时,我忘记添加我想要的amount列。我该如何在模型之后添加它?
2个回答

17

通过控制台创建新的迁移:

rails g migration add_amount_to_items

这应该会创建一个类似于以下内容的迁移:

class AddAmountToItems < ActiveRecord::Migration
  def change
    # add_column table_name, :column_name, :column_type
    add_column :items, :amount, :integer
  end
end

你好。我像这样做了:“rails g migration add_priority_to_article” - 但是生成的迁移文件没有指令...相反,我使用了“priority:integer”(结束g命令),它起作用了。 - Valter Ekholm

10

简单粗暴的方法:

rails g migration add_amount_to_items amount:integer

嗨,谢谢@WeGoingNowhere - 什么是适当、勤奋的方式,可以长期避免麻烦。如果你不想要“快速而肮脏”的方法呢? - BenKoshy
不要忘记运行迁移:bin/rake db:migrate - Arthur

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接