我正在尝试在控制器上实现删除,但是出现此错误:
违反完整性约束:1451无法删除或更新父行:外键约束失败
(`greatsup_wz`.`entryitems`, CONSTRAINT `entryitems_fk_check_entry_id` FOREIGN KEY (`entry_id`) REFERENCES `entries` (`id`)) (SQL: delete from `greatsup_wz`.`entries` where `id` = 686)
这是我的一部分代码:
$entryTable = DB::table(config("app.DB_ACCOUNTING").".entryitems")
->where('entryref_id','=',100)->get();
foreach($entryTable as $entryTbl):
DB::table(config("app.DB_ACCOUNTING").".entries")
->where('id','=',$entryTbl->entry_id)->delete(); // this is where my error occurred
endforeach;
我该如何解决?
答案 0 :(得分:0)
您添加的约束将防止数据被删除。您需要更新数据库架构。您需要在条目项迁移中添加onDelete('cascade')
Schema::create('tablename', function (Blueprint $table) {
$table->integer('columnName')->unsigned();
$table->foreign('columnName')->references('columnName')->on('relatedTableName')->onDelete('cascade');
});