LoginSignup
2
5

More than 5 years have passed since last update.

Rails で外部制約付きのカラムを削除する

Last updated at Posted at 2017-11-09
t.references :user, foreign_key: true, null: false

のように作った外部制約付きのカラムを削除する。

環境

  • Ruby 2.4.2
  • Rails 5.1.4
  • MySQL 5.7.x

Tasks テーブルの user_id を削除する場合

$ bin/rails g migration RemoveUserIdFromTasks
xxxxxxxxxxxxxx_remove_user_id_from_tasks.rb
class RemoveUserIdFromTasks < ActiveRecord::Migration[5.1]
  def change
    remove_reference :tasks, :user, foreign_key: true
  end
end

この状態で

$ bin/rails db:migrate

すればOK。

2
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5