エラー内容
$ php artisan migrate
Migrating: 2022_01_29_163203_create_projects_table
Illuminate\Database\QueryException
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'projects' already exists (SQL: create table `projects` (`id` bigint unsigned not null auto_increment primary key) default character set utf8mb4 collate 'utf8mb4_unicode_ci')
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:712
708▕ // If an exception occurs when attempting to run a query, we'll format the error
709▕ // message to include the bindings with SQL, which will make this exception a
710▕ // lot more helpful to the developer instead of just the database's errors.
711▕ catch (Exception $e) {
➜ 712▕ throw new QueryException(
713▕ $query, $this->prepareBindings($bindings), $e
714▕ );
715▕ }
716▕ }
+9 vendor frames
10 database/migrations/2022_01_29_163203_create_projects_table.php:32
Illuminate\Support\Facades\Facade::__callStatic("create")
+21 vendor frames
32 artisan:37
Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
まず結論(解決方法)
mysqlに既に他のlaravelプロジェクトでテーブル出来上がっているため、そのテーブルを消すことでmigrateが通るようになります。
なぜ記事を書いたのか
久しぶりにdockerを使わないでlaravelの環境構築したところ、みたことのない種類のエラーに出会い躓きました。
そのためぱっと見どこにエラー文が書いてあるかわからず、解決まで時間がかかったので備忘録の意味も含めて記載しようと思いました。
エラー文はどこにあるのか
筆者は最初は ➜ 712▕ throw new QueryException(
を見て、その文字で検索して結局解決できず、試行錯誤して偶然解決までたどり着きました。
解決後にエラーをよくよくみたところ SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'projects' already exists ~
というエラー文があり、「既にテーブルが入っている」と言っていたことに気づきました。
結論ですが、エラー文はSQLSTATE[42S01]:~
の部分に書いてあります。
まとめ
今回の記事でお伝えしたかったことは下記の2点です。
-
エラー解決の具体例を探している場合:DBに他のプロジェクトのテーブルが入っていないか確認する
-
エラー文が何を言っているかわからず困っている場合:
SQLSTATE[42S01]:~
の部分を読んでみる