1215无法添加外键约束Laravel 9迁移



我运行以下迁移代码。错误后返回

错误:

1215 Cannot add foreign key constraint (SQL: alter table `company_address_user` add c  
onstraint `company_address_user_user_id_foreign` foreign key (`user_id`) references `users` (`id`))

代码

Schema::create('company_address_user', function (Blueprint $table) {
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('company_address_id');
$table->primary(['user_id', 'company_address_id']);
});
Schema::table('company_address_user', function (Blueprint $table) {
$table->foreign(['user_id'])->references(['id'])->on('users');
$table->foreign(['company_address_id'])->references(['id'])->on('company_addresses');
});
Schema::create('company_address_user', function (Blueprint $table) {
$table->foreignId('user_id')
->references('id')
->on('users')
->cascadeOnDelete();
$table->foreignId('company_address_id')
->references('id')
->on('company_addresses')
->cascadeOnDelete();
$table->unique(['user_id', 'company_address_id']);
});

最新更新