Help us understand the problem. What is going on with this article?

Laravel SQLiteからMySQLに変更したら外部キー制約のマイグレーションが失敗してハマった

>php artisan -V
Laravel Framework 6.1.0

以下のusersに対して、user_idを外部キー制約として持つsubscribesテーブルを作りました。

Schema::create('users', function (Blueprint $table) {
  $table->bigIncrements('id');
  $table->string('name');
  $table->string('email')->unique();
  $table->timestamp('email_verified_at')->nullable();
  $table->string('password');
  $table->rememberToken();
  $table->timestamps();
});

SQLiteで動作していたマイグレーションだったので、そのままphp artisan migrateしたらエラーになりました。

実行したマイグレーション
Schema::create('subscribes', function (Blueprint $table) {
  $table->bigIncrements('id');
  $table->integer('user_id')->unsigned();
  $table->string('channel_id');
  $table->string('channel_title');
  $table->timestamps();

  // userが削除されたとき、それに関連するも一気に削除する制約
  $table->foreign('user_id')
        ->references('id')
        ->on('users')
        ->onDelete('cascade');
        });
Illuminate\Database\QueryException:
 SQLSTATE[HY000]: General error: 1215 
 Cannot add foreign key constraint
 (SQL: alter table `subscribes` add constraint 
 `subscribes_user_id_foreign` foreign key (`user_id`) 
 references `users` (`id`) on delete cascade) 

PHP - 【Laravel】外部キー制約があるテーブルのmigrateができません|teratail という記事をみて、user_idカラムを作るときの記述が異なることに気づいてその部分を修正することで、解決しました!

修正点
-  $table->integer('user_id')->unsigned();
+  $table->unsignedBigInteger('user_id');
正しいマイグレーション
Schema::create('subscribes', function (Blueprint $table) {
  $table->bigIncrements('id');
  $table->unsignedBigInteger('user_id');
  $table->string('channel_id');
  $table->string('channel_title');
  $table->timestamps();

  // userが削除されたとき、それに関連するも一気に削除する制約
  $table->foreign('user_id')
        ->references('id')
        ->on('users')
        ->onDelete('cascade');
        });

型が違っているからエラーになったっぽいのですが、それじゃあSQLiteはなぜ通った?という疑問が…。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away