完整性约束违规:1451无法删除或更新父行:外键约束失败 - Laravel
运行PHP工匠迁移
后,我会遇到此错误。
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (SQL: drop table if exists `products`)
create_products_table.php
public function up()
{
Schema::dropIfExists('products');
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
// Other attributes here
$table->bigInteger('ctg_id')->unsigned();
$table->bigInteger('product_variation_id')->unsigned();
$table->bigInteger('sub_ctg_id')->unsigned();
$table->bigInteger('vehicle_id')->unsigned();
$table->foreign('ctg_id')->references('id')->on('ctgs')->constrained()->onDelete('cascade')->onUpdate('cascade');
$table->foreign('product_variation_id')->references('id')->on('product_variations')->constrained()->onDelete('cascade')->onUpdate('cascade');
$table->foreign('sub_ctg_id')->references('id')->on('sub_ctgs')->constrained()->onDelete('cascade')->onUpdate('cascade');
$table->foreign('vehicle_id')->references('id')->on('vehicles')->constrained()->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('products');
}
我在线搜索,获得了此解决方案,相应地更改了代码,但徒劳无功 https://stackoverflow.com/a/47544195/7290043
现在,我我只是用下表工作
I'm getting this error, upon running php artisan migrate
.
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (SQL: drop table if exists `products`)
create_products_table.php
inside migration
public function up()
{
Schema::dropIfExists('products');
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
// Other attributes here
$table->bigInteger('ctg_id')->unsigned();
$table->bigInteger('product_variation_id')->unsigned();
$table->bigInteger('sub_ctg_id')->unsigned();
$table->bigInteger('vehicle_id')->unsigned();
$table->foreign('ctg_id')->references('id')->on('ctgs')->constrained()->onDelete('cascade')->onUpdate('cascade');
$table->foreign('product_variation_id')->references('id')->on('product_variations')->constrained()->onDelete('cascade')->onUpdate('cascade');
$table->foreign('sub_ctg_id')->references('id')->on('sub_ctgs')->constrained()->onDelete('cascade')->onUpdate('cascade');
$table->foreign('vehicle_id')->references('id')->on('vehicles')->constrained()->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('products');
}
I searched online, got this solution, changed the code accordingly but in vain
https://stackoverflow.com/a/47544195/7290043
For now, I'm just working with the below tables
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须在删除表之前使用
$ table-> dropforeign()
,请删除外部键以避免此错误。You must use
$table->dropForeign()
before deleting the table, Remove the external key to avoid this error.