【发布时间】:2022-11-01 13:22:09
【问题描述】:
我有一个本地开发的带有 sqlite 的 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)
迁移如下所示:
测试1
public function up()
{
Schema::create('test1s', function (Blueprint $table) {
$table->id();
...
测试2
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();
但这不起作用。
有人可以指出我正确的方向吗?谢谢
【问题讨论】: