1
2

More than 3 years have passed since last update.

あとから外部キーを設定するには?

Posted at

1対多において、あとから外部キーを設定する。

前提条件

投稿機能作成済み(今回はtaskアプリ)
User機能作成済み

$ rails g migration AddUserRefToTasks

マイグレーションファイルを編集

add_user_ref_to_tasks.rb
add_reference :tasks, :user, foreign_key: true

taskモデルとuserモデルを編集

task.rb
#省略
belongs_to :user
#省略
user.rb
#省略
has_many :tasks
#省略
1
2
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
1
2