SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (demopurpose_fundraising.campaign_product, CONSTRAINT campaign_product_campaign_id_foreign FOREIGN KEY (campaign_id) REFERENCES campaign (id)) (SQL: delete from campaign where id = 60)
事件表架构:
Schema::create('campaign', function (Blueprint $table) {
$table->engine='InnoDB';
$table->increments('id');
$table->integer('users_id')->unsigned();
$table->foreign('users_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->string('campaign_name');
$table->float('campaign_goal',8,2);
$table->string('discription',400);
$table->string('image');
$table->string('category');
$table->date('start_date');
$table->date('end_date');
$table->float('total_fund',8,2);
});
campaign_product 表架构:
Schema::create('campaign_product', function (Blueprint $table) {
$table->engine='InnoDB';
$table->increments('id');
$table->integer('campaign_id')->unsigned();
$table->foreign('campaign_id')->references('id')->on('campaign')->onDelete('cascade')->onUpdate('cascade');
$table->integer('product_id')->unsigned();
$table->foreign('product_id')->references('id')->on('product')->onDelete('cascade')->onUpdate('cascade');
});
campaign_id是campaign_product删除中的外键..
我想删除广告系列..
如何删除营销事件产品然后营销事件?
最佳答案
除非您从 campaign_product
表中删除这些 campaign_id
,否则您无法删除它。在删除广告系列之前,请先从 campaign_product
表中分离产品。示例:
$campaign = Campaign::query()->findOrFail($id); //find campaign
$campaign->products()->detach($campaign->product); //detach products from `campaign_products` table
$campaign->delete(); //delete the campaign
了解有关分离多对多关系记录的更多信息:https://laravel.com/docs/5.6/eloquent-relationships#updating-many-to-many-relationships
关于php - 从营销事件表中,我想删除营销事件,但无法删除..发生以下错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49381095/