laravel delete(完整性约束冲突)

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

我试图在控制器上实现删除,但出现以下错误:
完整性约束冲突:1451无法删除或更新父行:外键约束失败 (greatsup_wz.entryitems, CONSTRAINTentryitems_fk_check_entry_idFOREIGN KEY (entry_id) REFERENCESentries(id)) (SQL: delete fromgreatsup_wz.entrieswhereid= 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;

我该怎么修?

y3bcpkx1

y3bcpkx11#

您添加的约束将阻止删除数据。您需要更新数据库架构。您需要在entryitems迁移中添加ondelete('cascade')

Schema::create('tablename', function (Blueprint $table) {

    $table->integer('columnName')->unsigned();
    $table->foreign('columnName')->references('columnName')->on('relatedTableName')->onDelete('cascade');
});

相关问题