Laravel SQLSTATE [23000]:违反完整性约束:1451

时间:2018-08-28 10:23:25

标签: php laravel

在一张桌子上删除功能存在一些问题。我需要一个函数来处理外键约束的异常。 必须修复该功能,以便我可以添加softdelete。 我尝试从同一张表中复制一列,删除所有内容时效果很好,但是有2列无法删除。 我将在这里放置删除功能。 如果您需要更多来自软件的代码,请通知我,但是我想这就是您所要知道的问题所在。

  

错误消息:pastebin.com/CktBbbaX(由于某些原因无法发布到堆栈上)

这是php函数:

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

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

费率表,如您所见,我已经具有删除的CASCADE函数,但仍然无法正常工作。

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');
    }

这是最后一张桌子的翻译。 (再次在堆栈上实现代码时遇到问题)

  

pastebin.com/h15Qe5mR

0 个答案:

没有答案