laravel sql[23000]删除错误

z0qdvdin  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(379)

一个表上的delete函数有一些问题。我需要一个函数来处理外键约束的异常。必须修复该函数,以便我可以添加softdelete。我试图从同一个表中复制一列,当我删除所有内容时都很好,但是有两列我无法删除。我将把删除函数放在这里。如果你需要更多的软件代码请告诉我,但我认为这是所有你需要知道的问题是什么。
错误消息:pastebin.com/cktbbbax(由于某些原因无法在堆栈上发布)
这是php函数:

public function destroy($id)
        {
            Rate::findOrFail($id)->delete();

            return redirect()->back()->with('status', __('messages/success.delete'));
        }

费率表:

public function up()
    {
        Schema::create('rates', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('supplier_id')->unsigned()->nullable();
            $table->integer('house_id')->unsigned()->nullable();
            $table->boolean('b4g_edit');
            $table->integer('derived_qty')->unsigned()->nullable();
            $table->integer('derived_price')->unsigned()->nullable();
            $table->tinyInteger('limit_quantity')->default(0);
            $table->decimal('round_prices', 10, 3);

            // TRANSLATABLE
            $table->integer('name_frontend')->unsigned();
            $table->integer('name_backend')->unsigned();
            $table->integer('description')->unsigned();
            // END TRANSLATABLE

            $table->timestamps();
            $table->integer('created_by')->unsigned()->nullable();
            $table->integer('updated_by')->unsigned()->nullable();

            $table->foreign('supplier_id')->references('id')->on('suppliers')->onDelete('SET NULL');
            $table->foreign('house_id')->references('id')->on('houses')->onDelete('SET NULL');
            //translation foreign keys
            $table->foreign('name_frontend')->references('group_id')->on('translations')->onDelete('CASCADE');
            $table->foreign('name_backend')->references('group_id')->on('translations')->onDelete('CASCADE');
            $table->foreign('description')->references('group_id')->on('translations')->onDelete('CASCADE');         
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('rates');
    }

这是价格部分:

public function up()
    {
        Schema::create('rate_parts', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('rate_id')->unsigned();
            $table->enum(
                'product_type',
                [ 'overnight','transport','activity','addon' ]
            );
            $table->integer('product_id')->unsigned();
            $table->date('date_from');
            $table->date('date_to');
            $table->enum(
                'type',
                [
                    'dummy',
                    'quantity',
                    'price',
                    'closed',
                    'cta',
                    'ctd',
                    'minstay',
                    'start',
                    'expire',
                    'min',
                    'max',
                    'max_beds',
                    'persons',
                    'daily_price',
                    'km_price',
                    'pricechange_abs',
                    'pricechange_rel'
                ]
            );
            $table->decimal('value', 10, 2);

            $table->timestamps();
            $table->integer('created_by')->unsigned()->nullable();
            $table->integer('updated_by')->unsigned()->nullable();

            $table->index(['product_id', 'product_type']);
            $table->foreign('rate_id')->references('id')->on('rates');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('rate_parts');
    }
wvt8vs2t

wvt8vs2t1#

如果表a有外键引用表b的列(例如表\u b \u id),则不能删除被引用的表b行,因为存在与外键的依赖关系。您需要做的是删除指向表b行的引用行。
https://stackoverflow.com/a/34501633/4925008
从一个与你类似的问题中检查这个答案。你要么 ON DELETE CASCADE 或者在删除要删除的主行之前手动删除该行

相关问题