クマのブログ

つまづいたところ、学びを書いていきます

php artisan migrateで作成したカラム数が足りない

前提

  • Windows

  • Laravel : 6.0

  • MySQL : 5.7

  • 実務で使用予定のLaravelをTechpitで学習中につまずいた

  • 使用エディタ:cloud9

状況

MySQLのカラムを8個生成したかったので、

↓ database/migration/(timestamp)_create_reviews_table.php

・
・
・
    public function up()
    {
        Schema::create('reviews', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
            $table->string('title');
            $table->longText('body');
            $table->string('image')->nullable(); 
            $table->tinyInteger('status')->default(1)->comment('0=下書き, 1=アクティブ, 2=削除済み');
            $table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP'));
            $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
        });
    }
・
・
・

をLaravelで記述し、

php artisan migrate

をターミナルで実行。

↓実行結果

(ユーザー名):~/environment/books $ php artisan migrate
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (0.03 seconds)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (0.02 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (0.01 seconds)
Migrating: 2021_04_13_124816_create_reviews_table
Migrated:  2021_04_13_124816_create_reviews_table (0.04 seconds)

とエラーが出ずに処理されたので、生成されたカラムを確認すべくMySQLへログイン。

(ユーザー名):~/environment/books $ mysql -uroot -p******
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.31 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

カラムを作ったデータベースを読み込み、データベースを確認した。

mysql> use books;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed

mysql> SHOW FULL COLUMNS FROM reviews;

困ったこと

mysql> SHOW FULL COLUMNS FROM reviews;

の結果、予想した出力結果と実際の結果が異なった

↓予想した出力結果(狙い値)

f:id:kuma_kuma0121:20210414213613p:plain

+------------+---------------------+--------------------+------+-----+-------------------+-----------------------------+---------------------------------+------------------------------------------------+
| Field      | Type                | Collation          | Null | Key | Default           | Extra                       | Privileges                      | Comment                                        |
+------------+---------------------+--------------------+------+-----+-------------------+-----------------------------+---------------------------------+------------------------------------------------+
| id         | bigint(20) unsigned | NULL               | NO   | PRI | NULL              | auto_increment              | select,insert,update,references |                                                |
| user_id    | bigint(20) unsigned | NULL               | NO   | MUL | NULL              |                             | select,insert,update,references |                                                |
| title      | varchar(255)        | utf8mb4_unicode_ci | NO   |     | NULL              |                             | select,insert,update,references |                                                |
| body       | longtext            | utf8mb4_unicode_ci | NO   |     | NULL              |                             | select,insert,update,references |                                                |
| image      | varchar(255)        | utf8mb4_unicode_ci | YES  |     | NULL              |                             | select,insert,update,references |                                                |
| status     | tinyint(4)          | NULL               | NO   |     | 1                 |                             | select,insert,update,references | 0=下書き, 1=アクティブ, 2=削除済み             |
| updated_at | timestamp           | NULL               | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | select,insert,update,references |                                                |
| created_at | timestamp           | NULL               | NO   |     | CURRENT_TIMESTAMP |                             | select,insert,update,references |                                                |
+------------+---------------------+--------------------+------+-----+-------------------+-----------------------------+---------------------------------+------------------------------------------------+
**8 rows** in set (0.00 sec)

8個のカラム

↓実際の出力結果

f:id:kuma_kuma0121:20210414213419p:plain

+------------+---------------------+--------------------+------+-----+-------------------+-----------------------------+---------------------------------+------------------------------------------------+
| Field      | Type                | Collation          | Null | Key | Default           | Extra                       | Privileges                      | Comment                                        |
+------------+---------------------+--------------------+------+-----+-------------------+-----------------------------+---------------------------------+------------------------------------------------+
| id         | bigint(20) unsigned | NULL               | NO   | PRI | NULL              | auto_increment              | select,insert,update,references |                                                |
| updated_at | timestamp           | NULL               | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | select,insert,update,references |                                                |
| created_at | timestamp           | NULL               | NO   |     | CURRENT_TIMESTAMP |                             | select,insert,update,references |                                                |
+------------+---------------------+--------------------+------+-----+-------------------+-----------------------------+---------------------------------+------------------------------------------------+
**3 rows** in set (0.00 sec)

3個のカラム

※補足 'id':Laravelインストール時の初期カラム 'update_at', 'created_at':追加したカラム

やったこと①

教材を進める上でヌケ・モレがあったか?

仮説

「手順を飛ばした」「タイポ」等により、自分の見落としによるものがあったか再度確認

対策

特にタイポはよくあるので(↓こんな感じで…)、教材のLaravelのコードをそのままコピペしたり(あまり好まないが)、何回も教材を見返した。

結果:×

特にヌケ・モレはなく、正しく記述していたので解決に至らず。

やったこと②

キャッシュ等が残っていたりしたか

仮説

キャッシュが残っていた、もしくはconfigが残っていた等が原因か、と推測

対策

php artisan cache:clear
php artisan config:clear

を実行し、更にサーバーを起動し、

php artisan serve --host=$IP --port=$PORT

すぐに切断した後

php artisan migrate

実行

結果:×

php artisan migrateの出力結果は

No migrations table
※確かこの英語…

となり、依然この状態↓

f:id:kuma_kuma0121:20210414213419p:plain

やったこと③

一度データベースをきれいにしてみよう

仮説

「中途半端な3個のカラムがあるからダメなのでは?」と推測

対策

MySQLにも反映されているデータベースを一度きれいにするために

php artisan migrate:rollback --step=1

を繰り返し行い、データベースを一旦リセット。

php artisan migrate:status

の結果が下記の通り、"Ran"の部分が全て"N"にしたら

+------+----------------------------------------+
| Ran? | Migration                              |
+------+----------------------------------------+
| N   | (timestamp)_create_***_*** |
| N    | (timestamp)_create_***        |
| N   | (timestamp)_***         |
| N    | (timestamp)_***        |
+------+----------------------------------------+

その状態で再度マイグレーション実行。

php artisan migrate

↓実行結果 f:id:kuma_kuma0121:20210414214654p:plain

いい感じに通ったかも?

結果:〇

MySQLへログインし、データベースを確認をすると…

f:id:kuma_kuma0121:20210414214827p:plain

狙い値通り、データベースのカラムが生成されている。

備考

・そもそもなぜカラムを生成するLaravelのコードは間違っていなかったのに、結果として3つしかカラムが生成されなかったのか がわかっていない ・なぜrollbackで解決したのか が不明点であるため、わかれば幸い。

あと、rollbackではなく、

php artisan fresh(refresh)

でもいけたのでは?と思ったり。