LoginSignup
0

posted at

【Laravelエラー】Cannot drop table '' referenced by a foreign key constraint

環境

Laravel v9.5.1 (PHP v8.1.3)

状況

usersテーブルをphp artisan migrate:rollbackしたときのエラー。

Cannot drop table 'users' referenced by a foreign key constraint 'posts_user_id_foreign' on table 'posts'. (SQL: drop table if exists `users`)

原因

usersテーブルにpost_idが外部キーとして設定されており、postsテーブルよりusersテーブルを先にrollbackしようとしたため。

解決法

migrationファイルで外部キーを削除する。

public function down()
{
  Schema::table('posts', function (Blueprint $table) {
     $table->dropForeign('posts_user_id_foreign');
  });
}

再度php artisan migrate:rollbackでrollback!

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
What you can do with signing up
0