我对即将发布的版本进行了一系列重构迁移。一些迁移用于移动列。
当我一次性运行迁移时,以下示例中的特定列永远不会复制到新位置。
def up
add_column :buy_topics, :price, :integer
say_with_time 'Move price and value to buy/topics' do
bar = ProgressBar.new(Buy::Topic.count)
Buy::Topic.includes{topic}.find_each do |topic|
topic.price = topic.topic.price
topic.save
bar.increment!
end
end
remove_column :topics, :price
end
即使没有复制这些值,仍会成功创建和删除列。
但是,如果我将所有迁移运行到此前的迁移(例如rake db:migrate VERSION=XXXXXXXXn-1
),然后手动运行此特定迁移(rake db:migrate VERSION=XXXXXXXXn
),则会复制值。< / p>
总而言之,如果我单独运行该迁移它会起作用,否则它将无效
这背后的原因是什么?
答案 0 :(得分:1)
尝试这样做:
Buy::Topic.reset_column_information
有关详细信息,请参阅[本页] [http://api.rubyonrails.org/classes/ActiveRecord/Migration.html]上的“在更改表格后使用模型”。