Rails 3中未定义的add_column

Art*_*t F 2 ruby ruby-on-rails rails-migrations

我试图在Rails 3中运行迁移,我希望在表中添加一列,代码如下所示:

class AddConstAdr < ActiveRecord::Migration
  def change
    change_table: constants do |t|
      t.add_column :home_address, :string
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

当我这样做时,rake db:migrate我得到一个错误,说明未定义的方法'add_column'.我很困惑为什么会这样,有人可以帮忙吗?

mu *_*ort 12

您似乎混合了两种不同的迁移方式.你可能想要这个:

def change
  change_table :constants do |t|
    t.string :home_address
  end
end
Run Code Online (Sandbox Code Playgroud)

或这个:

def change
  add_column :constants, :home_address, :string
end
Run Code Online (Sandbox Code Playgroud)

两种形式都应该做同样的事情:home_addressconstants表中添加一个字符串列.

我也假设你change_table: constants只是一个错字change_table :constants.

可以在" 迁移指南"中找到更多信息.