LoginSignup
0
0

More than 1 year has passed since last update.

【Laravelエラー】Failed to open the referenced table

Posted at

環境

Laravel v9.5.1 (PHP v8.1.3)

状況

php artisan migrate実行したときに下記のエラー。

Failed to open the referenced table 'users' (SQL: alter table `posts` add constraint `posts_user_id_foreign` foreign key (`user_id`) references `users` (`id`))

原因

外部キーを設定しているmigrationファイルがあるときに、「postsテーブルがuser_idを持っている状態で、usersテーブルを参照したいけどできない」というエラー。
migrationファイルを生成したときの日付がusersテーブルよりpostsテーブルが先になってしまっていた。

解決法

migrationファイルの日付を親ファイルが子ファイルより先になるように修正する。

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