Heroku Rails数据迁移之谜

Heroku Rails数据迁移之谜,heroku,ruby-on-rails-4,data-migration,Heroku,Ruby On Rails 4,Data Migration,我有一个银行账户模型和一个信用卡模型。目前,它们都有一个客户id和一个公司id。我正试图将它们转换为一个多态关联为“所有者”。我在模型中添加了适当的关系,然后为所有当前客户和公司编写了数据迁移。那个迁移看起来像这样 class AddOwnersToCardsAndBanks < ActiveRecord::Migration class Card < ActiveRecord::Base; end class BankAccount < ActiveRecord::B

我有一个银行账户模型和一个信用卡模型。目前,它们都有一个客户id和一个公司id。我正试图将它们转换为一个多态关联为“所有者”。我在模型中添加了适当的关系,然后为所有当前客户和公司编写了数据迁移。那个迁移看起来像这样

class AddOwnersToCardsAndBanks < ActiveRecord::Migration
  class Card < ActiveRecord::Base; end
  class BankAccount < ActiveRecord::Base; end

  def change
    # Card related changes
    add_column :cards, :owner_id, :integer
    add_column :cards, :owner_type, :string

    # BankAccount related changes
    add_column :bank_accounts, :owner_id, :integer
    add_column :bank_accounts, :owner_type, :string

    Card.all.each do |card|
      if card.try(:customer_id)
        card.update_attribute(:owner_id, card.patient_id)
        card.update_attribute(:owner_type, "Patient")
      end
    end

    BankAccount.all.each do |bank_acount|
      if bank_acount.try(:customer_id)
        bank_acount.update_attribute(:owner_id, bank_acount.customer_id)
        bank_acount.update_attribute(:owner_type, "Patient")
      end
      if bank_acount.try(:company_id)
        bank_acount.update_attribute(:owner_id, bank_acount.company_id)
        bank_acount.update_attribute(:owner_type, "Company")
      end
    end

    remove_column :cards, :customer_id, :integer
    remove_column :bank_accounts, :customer_id, :integer
    remove_column :bank_accounts, :company_id, :integer
  end
end
class AddOwnersToCardsAndBanks
这(看起来)在本地有效。我将其推送到功能服务器,甚至控制台都记录了迁移过程中发生的事情。似乎在迁移过程中,一切都做了它应该做的事情(这意味着我在更新银行帐户后记录了该帐户,并将属性打印在我期望的位置)。然而,当我在FeatureServer上运行rails控制台并查找更改后的数据时,它似乎只适用于卡,而不适用于银行帐户

具体来说,事后数据库中的银行帐户上从未实际设置过所有者id和所有者类型。但当我从迁移中记录这些属性时,它们就如预期的那样存在了。这与卡片相反,卡片上的一切都被正确设置,数据库显示了这一点。我完全糊涂了


看起来可能和Heroku或Circle有关,但我不知道。如果您对可能出现的问题有任何想法,我们将不胜感激。

有时在数据迁移过程中,您无法写入刚刚创建的列。尝试将其添加到迁移脚本:

# BankAccount related changes
add_column :bank_accounts, :owner_id, :integer
add_column :bank_accounts, :owner_type, :string

Card.reset_column_information
BankAccount.reset_column_information

Card.all.each do |card|
更多信息可在此处找到: