1
1

More than 1 year has passed since last update.

DBにreferences型のカラムが追加できない

Posted at

起きた問題

マイグレーションファイルにrefereces型のカラムを追加したかったが、マイグレーションできなかった。

試したこと

全てロールバックした後、DBを削除し作り直し、マイグレーション。←ダメ
試しにreferenes型のカラムだけマイグレーションファイルから消してマイグレーション。 ←できる

解決方法

参照先:箱のプログラミング日記。
ありがとうございます:bow_tone3:

rails g migration Addカラム名Toテーブル名 追加したいカラム名:カラム型でマイグレーションファイルを生成。

中身を以下の内容に書き換え。

class AdduserToPrototypes < ActiveRecord::Migration[6.0]
  def change
    change_table :Prototypes do |t|
      t.references :user, foreign_key: true, presence: true
    end
  end
end

これでマイグレーションできました。

1
1
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
1