删除cakephp中的外键

时间:2015-12-28 06:26:41

标签: mysql cakephp orm foreign-keys cakephp-2.x

我想在数据库中删除一个外键的记录。以下代码是为产品表编写的。

public function delete() {
    $id = $this->request->params['pass'][0];
    if( $this->Product->delete( $id )){
        $this->Session->setFlash('Product was deleted');
        $this->redirect(array('action'=>'index'));
    } else {    
        $this->Session->setFlash('Unable to delete product');
        $this->redirect(array('action' => 'index'));
    }
}

现在我该怎样做才能从主表中删除它?

这是我得到的错误:

Error: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`indianautogas1`.`agent_to_consumer`, CONSTRAINT `agent_to_consumer1_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`))
SQL Query: DELETE `Product` FROM `indianautogas1`.`products` AS `Product` WHERE `Product`.`id` = 1

0 个答案:

没有答案