请教一下外键约束出现的问题,提示不兼容,应该怎么解决呢?

请教一下大家。我添加外键约束

Schema::table('supply_infos', function (Blueprint $table) {
// 当 user_id 对应的 users 表数据被删除时,删除供应信息
$table->unsignedInteger('user_id')->change();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});

出现这个问题。提示是不兼容。

  SQLSTATE[HY000]: General error: 3780 Referencing column 'user_id' and referenced column 'id' in foreign key constraint 'supply_infos_user_id_foreign' are incompatible. (SQL: alter table `supply_infos` add constraint `supply_infos_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade)

user表的字段是这样的

$table->id();

supply_infos表的字段是这样的

$table->integer('user_id')->comment('所属用户');

字段类型

user表是bigint类型
其他表的user_id 是int类型

请问应该怎么解决呢

乌鸦嘴新手社区 wyz.xyz 为技术新手提供服务
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
Summer
最佳答案

字段类型要保持一致。

$table->unsignedInteger('user_id')->change();

改为:

$table->unsignedBigInteger('user_id')->change();
2年前 评论
讨论数量: 2
Summer

字段类型要保持一致。

$table->unsignedInteger('user_id')->change();

改为:

$table->unsignedBigInteger('user_id')->change();
2年前 评论

谢谢@Summer 的回复。出现了新的问题

  SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`nxsupply`.`#sql-1c54_8ab`, CONSTRAINT `supply_infos_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE) (SQL: alter table `supply_infos` add constraint `supply_infos_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade)
2年前 评论
shebaoting (作者) (楼主) 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!