LoginSignup
0
0

More than 1 year has passed since last update.

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

Posted at

環境

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!

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