我有一个本地开发的laravel项目。出于部署原因,我想切换到mysql。不幸的是,我的关系迁移不再工作了,并产生了以下错误(我已经确保它们运行的顺序是正确的,所有其他必需的表首先生成并且看起来是正确的)
Can't create table `laraveltest`.`test1s_test2s` (errno: 150 "Foreign key constraint is incorrectly formed")
(SQL: alter table `test1s_test2s` add constraint `test1s_test2s_test1_id_foreign` foreign key (`suacap_id`)
references `test1s` (`id`) on delete cascade)
迁移过程如下:
test1
public function up()
{
Schema::create('test1s', function (Blueprint $table) {
$table->id();
...
test2
public function up()
{
Schema::create('test2s', function (Blueprint $table) {
$table->id();
...
关系表test1s_test2s
public function up()
{
Schema::create('test1s_test2s', function (Blueprint $table) {
$table->primary(['test1_id', 'test2_id']);
$table->string('test1_id');
$table->foreign('test1_id')
->references('id')
->on('test1s')->onDelete('cascade');
$table->string('test2_id');
$table->foreign('test2_id')
->references('id')
->on('test2s')->onDelete('cascade');
});
}
我猜这与其他表的bigInt id中没有签名的主键有关吧?我试着修改
$table->primary(['test1_id', 'test2_id'])->unsigned();
但这行不通。
有人能给我指明正确的方向吗?谢谢
发布于 2022-11-01 05:22:09
想一想,只要你做了什么外键,它们就应该是无符号的BIGINT而不是字符串,在Laravel 9-$table-> Foreign in (‘user_id’)中;也可以阅读正式文档拉勒维尔官方医生
https://stackoverflow.com/questions/74271215
复制相似问题