How do I reversibly remove_foreign_key on a table when the column doesn't match the table?

Viewed 1537

I tried the following

remove_foreign_key :users, :asset_types, column: :seek_asset_type_id

But got the error

StandardError: An error has occurred, this and all later migrations canceled:
wrong number of arguments (given 3, expected 1..2)

The documentation says it uses the same options as #add_foreign_key.

Documentation

The column was created previously with

add_reference :users, :seek_asset_type, foreign_key: {to_table: :asset_types}

This is the definition:

"fk_rails_4dcaa1c59c" FOREIGN KEY (seek_asset_type_id) REFERENCES asset_types(id)
4 Answers

This seems to be a deficiency in remove_foreign_key, but you can work around it by defining an add_foreign_key inverse action using reversible:

reversible do |dir|
  dir.up do
    remove_foreign_key :users, column: :seek_asset_type
  end
  dir.down do
    add_foreign_key :users, :asset_types, column: :seek_asset_type
  end
end

Please remove table_name

remove_foreign_key :users, column: :seek_asset_type_id

Try this I've tested it and work for me.

remove_foreign_key :users, name: "seek_asset_type_id"

Related