LoginSignup
8
6

More than 3 years have passed since last update.

【laravel】`base table or view already exists 1050 table 'users' already exists`が出た時の、php artisan migrate:fresh以外の解決法

Posted at

はじめに

 laravelでmigrationをタイポで間違えて、やり直した際に、base table or view already exists 1050 table 'users' already existsが出た際に、よくある解決法が、php artisan migrate:fresh

でも、ローカルのDBをリセットしたくない!! って思いませんか。
そんな思いを持った私が見つけたもう一つの解決方法です!!

ぜひ、php artisan migrate:freshをせずに、マイグレーションを成功させましょう!

前提

環境

PHP 7.4.16
Laravel 6.20.17
mysql 8.0.23
nginx 1.18.0

作りたいテーブル

location_tagテーブル

カラム名 外部キー
id int
location_id int
tag_id int

エラー文 (php artisan migrate 実行時)

   Illuminate\Database\QueryException  : SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'location_tag' already exists (SQL: create table `location_tag` (`id` bigint unsigned not null auto_increment primary key, `location_id` bigint not null, `tag_id` bigint not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

解決方法

1

php artisan tinkerで`tinker開く

2

テーブルが存在するか確認する

$Schema::hasTable("location_tag");
//location_tagはデーブル名
=> true

3

特定のテーブルだけ削除する

$Schema::drop("location_tag");

4

テーブルが消えたか確認する

$Schema::hasTable("location_tag");
//location_tagはデーブル名
=> false

5

tinkerを終了し、マイグレーションの状態を確認する

php artisan migrate:status

6

マイグレーションをrollbackする

php artisan migrate:rollback

7

再度、migrateする

php artisan migrate

これにて完了!!

参考

8
6
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
8
6