Laravel 7 迁移错误。无法添加外键约束
Laravel 7 migration error. Cannot add foreign key constraint
尝试在 Laravel 7 中创建外键,但是当我使用 artisan
迁移 tables 时出现错误
SQLSTATE[HY000]:一般错误:3780 引用列 'remote_id' 和外键约束 'products_remote_id_foreign' 中引用的列 'parent_id' 不兼容。 (SQL: alter table products
add constraint products_remote_id_foreign
foreign key (remote_id
) references categories
(parent_id
) on delete cascade )
我的分类table
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('parent_id');
$table->tinyInteger('depth');
$table->string('name');
$table->string('slug');
$table->text('description');
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->default(DB::raw('NULL ON UPDATE CURRENT_TIMESTAMP'))->nullable();
});
我的产品table
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('remote_id');
$table->foreign('remote_id')->references('parent_id')->on('categories')->onDelete('cascade');
$table->unsignedBigInteger('category_id');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->string('name');
$table->text('description');
$table->integer('price');
$table->tinyInteger('status')->default(1);
$table->integer('qty');
$table->string('barcode')->nullable();
$table->string('image');
$table->text('images')->nullable();
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->default(DB::raw('NULL ON UPDATE CURRENT_TIMESTAMP'))->nullable();
});
关于我做错了什么有什么想法吗?
感谢帮助!!!
正如 ynber 所提到的,要使外键兼容,它们必须属于同一类型。您的产品 table 中的 remote_id
是 unsignedBigInteger 而您尝试在类别 table、parent_id
中引用的键是一个 整数 。要解决此问题,请将产品 table 中的 category_id
更改为整数,或将类别 table 中的 parent_id
更改为 unsignedBigInteger。
编辑:
我仔细研究了外键并在另一个 post 上找到了 this 有用的答案。外键需要具有唯一约束或者是主键。由于您的 remote_id
列引用的 parent_id
既不是主键也没有唯一约束,因此您会收到此错误。
在另一个问题中给出的添加唯一约束的解决方案是运行这个命令(为你的tables修改):
alter table categories add constraint uq1 unique (parent_id);
尝试在 Laravel 7 中创建外键,但是当我使用 artisan
迁移 tables 时出现错误
SQLSTATE[HY000]:一般错误:3780 引用列 'remote_id' 和外键约束 'products_remote_id_foreign' 中引用的列 'parent_id' 不兼容。 (SQL: alter table products
add constraint products_remote_id_foreign
foreign key (remote_id
) references categories
(parent_id
) on delete cascade )
我的分类table
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('parent_id');
$table->tinyInteger('depth');
$table->string('name');
$table->string('slug');
$table->text('description');
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->default(DB::raw('NULL ON UPDATE CURRENT_TIMESTAMP'))->nullable();
});
我的产品table
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('remote_id');
$table->foreign('remote_id')->references('parent_id')->on('categories')->onDelete('cascade');
$table->unsignedBigInteger('category_id');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->string('name');
$table->text('description');
$table->integer('price');
$table->tinyInteger('status')->default(1);
$table->integer('qty');
$table->string('barcode')->nullable();
$table->string('image');
$table->text('images')->nullable();
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->default(DB::raw('NULL ON UPDATE CURRENT_TIMESTAMP'))->nullable();
});
关于我做错了什么有什么想法吗? 感谢帮助!!!
正如 ynber 所提到的,要使外键兼容,它们必须属于同一类型。您的产品 table 中的 remote_id
是 unsignedBigInteger 而您尝试在类别 table、parent_id
中引用的键是一个 整数 。要解决此问题,请将产品 table 中的 category_id
更改为整数,或将类别 table 中的 parent_id
更改为 unsignedBigInteger。
编辑:
我仔细研究了外键并在另一个 post 上找到了 this 有用的答案。外键需要具有唯一约束或者是主键。由于您的 remote_id
列引用的 parent_id
既不是主键也没有唯一约束,因此您会收到此错误。
在另一个问题中给出的添加唯一约束的解决方案是运行这个命令(为你的tables修改):
alter table categories add constraint uq1 unique (parent_id);