【问题标题】:Why do I have to run migration twice for values to appear in DB?为什么我必须运行两次迁移才能使值出现在数据库中?
【发布时间】:2010-04-07 19:38:48
【问题描述】:

莫名其妙地,当我使用 rake 运行以下迁移代码时,列而不是值出现在 MySQL DB 表中:

class AddTypeToItems < ActiveRecord::Migration
  def self.up
    add_column :items, 'type', :string, :limit => 100, :null => false

    Item.find_by_name('YUMMY_JUICE').update_attribute(:type, 'Juice')
    Item.find_by_name('TASTY_JUICE').update_attribute(:type, 'Juice')
    Item.find_by_name('NASTY_JUICE').update_attribute(:type, 'Juice')
  end

  def self.down
    remove_column :items, 'type'
  end
end

我实际上必须回滚迁移,然后再次运行它(总共两次)以显示值。怎么回事?

【问题讨论】:

    标签: mysql ruby-on-rails migration


    【解决方案1】:

    尝试添加

    Items.reset_column_information

    在您的 add_column 声明之后。 ActiveRecord 会缓存列信息,因此您需要在使用前重新加载。

    【讨论】:

    • 我只是想记住这个方法名!
    猜你喜欢
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多