LoginSignup
1
0

More than 3 years have passed since last update.

Laravel5で掲示板作成中にmigrateのBase table or view already existsのエラーにハマった

Last updated at Posted at 2019-06-16

Laravelはまだまだ日本では普及してないからエラー解消が厄介

Laravelがこれからくるだろうという予測のもと、Laraelの勉強がてら色々やっているが、まだまだ日本での普及度は低くエラーを解決する方法がなかなかみつからないのが厄介だ。

これからもっとLaravelが普及していくといいなと思いつつ、今後はQiitaにてLaravelに関する情報をまとめていこうかと思っている。

Laravel5.8で掲示板作成中に"php artisan migrate"の実行時のエラーにハマった。

Laravelでとりあえずチュートリアルをやろうと挑戦したのがこちら。

【Laravel 5.7対応】掲示板を作成するチュートリアル

しかしながら

$ php artisan make:migration create_posts_table --create=posts
$ php artisan make:migration create_comments_table --create=comments
$ php artisan migrate

この段階で早くもハマって全然抜け出せなかった。

php artisan migrateで発生したエラーは"Base table or view already exists"

test.php
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'comments' already exists (SQL: create table `comments` (`id` bigint unsigned not null auto_increment primary key, `post_id` int unsigned not null, `body` text not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_bin')

エラーを読むと、どうやら"comments"というテーブルが既に存在しているのではないかと書いている。

しかしながら、現在まだチュートリアルの段階で、commentsというエラーは作っていないし、database/migrationsの中身を見ても、それらしきもデータベースは存在しない。

もちろんappの中身を見てもそれ関連のモデルも存在しない。

それで最初にやったのがこちら。

laravel使うならmysqlのcollation指定はutf8mb4_binにしておくべき

mysqlのcollationが間違っているのではないかという記事を見つけて、実際に試してみたが、どうもうまくいかない。

結局"comments"という名前が悪かった

もしやと思い、"comments"という名前自体に問題があるのでないかと思って、この名前を変えてみることに・・・・

database/migrations/2019_06_09_091913_create_comments_table.php
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTestcommentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('testcomments', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('post_id');
            $table->text('body');
            $table->collation = 'utf8mb4_bin';
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('testcomments');
    }
}

まずはこのマイグレーション用のファイルを編集し、Calss名を"CreateTestcommentsTable"に変更、Schemaのテーブル名もup, down共に"testcomments"に変更。

名前はかっこ悪いがテスト的に変更してみた。

その後、このファイル名もdatabase/migrations/2019_06_09_091913_create_testcomments_table.phpに変更。

そして、migrateをゼロからやり直してみると。

$ php artisan migrate:refresh
Rolling back: 2019_06_09_091821_create_posts_table
Rolled back:  2019_06_09_091821_create_posts_table
Rolling back: 2019_05_26_082554_create_contacts_table
Rolled back:  2019_05_26_082554_create_contacts_table
Rolling back: 2014_10_12_100000_create_password_resets_table
Rolled back:  2014_10_12_100000_create_password_resets_table
Rolling back: 2014_10_12_000000_create_users_table
Rolled back:  2014_10_12_000000_create_users_table
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table
Migrating: 2019_05_26_082554_create_contacts_table
Migrated:  2019_05_26_082554_create_contacts_table
Migrating: 2019_06_09_091821_create_posts_table
Migrated:  2019_06_09_091821_create_posts_table
Migrating: 2019_06_09_091913_create_testcomments_table
Migrated:  2019_06_09_091913_create_testcomments_table

無事、migrationに成功しました!!

よかった。結局、commentsというのは名称として一般的すぎて、使えないということですね。

参考にしたチュートリアルはLaravel5.7で、今回利用したのはLaravel5.8だったので、もしかしたらバージョン変更に伴うエラーなのかもと思いつつ、なんとか前に進めそうでよかったです。

同じようなエラーに遭遇している人の役に立てば嬉しいです。

1
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
1
0