简体   繁体   中英

Remove foreign key from a table - Ruby on rails

I need to remove a foreign key from a table with migrations

This is the table where I have the foreign key:

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

      t.timestamps null: false
    end
  end
end

With this migration I create the foreign key:

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

With this I try to remove the foreign key

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

Did you try remove_foreign_key instead of remove_column as per the official doc ?

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

From official docs, check out more here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM