完整性约束违规:1451无法删除或更新父行:外键约束失败 - Laravel

发布于 2025-01-23 17:41:19 字数 2023 浏览 0 评论 0原文

运行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

enter image description here

For now, I'm just working with the below tables

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懷念過去 2025-01-30 17:41:19

您必须在删除表之前使用$ table-> dropforeign(),请删除外部键以避免此错误。

You must use $table->dropForeign() before deleting the table, Remove the external key to avoid this error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文