从表中删除外键-Ruby on Rails

时间:2019-01-04 05:09:24

标签: ruby-on-rails

我需要通过迁移从表中删除外键

这是我拥有外键的表:

class CreatePriceWorkshops < ActiveRecord::Migration
  def change
    create_table :price_workshops do |t|
      t.float :price

      t.timestamps null: false
    end
  end
end

通过此迁移,我创建了外键:

class AddTypeMoneysToPriceWorkshops < ActiveRecord::Migration
  def change
    add_reference :price_workshops, :type_money, index: true, foreign_key: true
  end
end

为此,我尝试删除外键

class RemoveTypeMoneyFromPriceWorkshops < ActiveRecord::Migration
  def change
    remove_column :price_workshops, :type_money_id, :integer
  end
end

2 个答案:

答案 0 :(得分:0)

您是否按照official doc尝试使用remove_foreign_key而不是remove_column

class RemoveTypeMoneyFromPriceWorkshops < ActiveRecord::Migration
  def change
    remove_foreign_key :price_workshops, :type_money
  end
end

答案 1 :(得分:0)

class RemoveTypeMoneyFromPriceWorkshops < ActiveRecord::Migration
  def change
    remove_reference(:price_workshops, :type_money, index: true, foreign_key: true)
  end
end

从官方文档中查看更多here