在迁移 [Laravel 8] 中添加外键时出错(错误号:150 "Foreign key constraint is incorrectly formed")

Error adding foreign key in migration [Laravel 8] (errno: 150 "Foreign key constraint is incorrectly formed")

我有我的users

Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->enum('role', ['admin', 'operator', 'owner']);
            $table->string('password');
            $table->foreignId('profile_id')->constrained('profile');
            $table->rememberToken();
            $table->timestamps();
        });

我有我的profile

Schema::create('profile', function (Blueprint $table) {
            $table->id();
            $table->string('full_name');
            $table->bigInteger('phone');
            $table->string('address');
            $table->enum('gender', ['men', 'women']);
        });

每次尝试迁移时都会弹出错误

SQLSTATE[HY000]: General error: 1005 Can't create table `myDatabase`.`users` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `users` add constraint `users_profile_id_foreign` foreign key (`profile_id`) references `profile` (`id`))

我听说并知道如果两种数据类型不同就会发生这个错误,因为我以前遇到过这个问题并设法通过确保两种数据类型相同来修复它。但我认为数据类型已经相同了吧?我错过了什么吗?或者问题不在于数据类型?谢谢!

此问题的已知原因是序列,如果您的“Users”迁移文件是 运行 在“Profile" 迁移文件,那么就会出现这种情况。

解决方案是更改迁移文件名,以便“配置文件”迁移文件首先运行。