LoginSignup
0
0

More than 3 years have passed since last update.

rails のマイグレーションで詰まった話

Last updated at Posted at 2020-05-10

Railsでマイグレートしているときに以下のエラーが出ました。
Mysql2::Error: Table 'DB名.テーブル名' doesn't exist: SHOW FULL FIELDS FROM `テーブル名

単純にテーブルが存在しないとのことで少し調べて見た所、外部キーを設定しているところがまずかったみたいです。例)t.references :テーブル名, foreign_key: true, null: false

つまり、
1 app/db/migrateにあるマイグレーションファイルは作成された時間順(ファイル名の先頭の数字が若い順)に実行される。

2 そこでまだ作っていないテーブルを参照しようとしていた。
例)
20200510000000_create_table1.rbで外部キーにtable2を設定していて、
20200510000001_create_table2.rbでtable2を作成している。

結論:
外部キーで参照する前にテーブルを作成しましょう!

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