[英]how can I make restrictions if I want to delete a data in the database and it is foreign key in codeigniter?
I have two tables named "location"
which consist of "location_id"
and "location_name"
and the other table is "computer"
which have the "location"
as a foreign key. 我有两个名为"location"
表,由"location_id"
和"location_name"
,另一个表是"computer"
,其中的"location"
为外键。 Now whenever I want to delete a data which is used in the table "computer"
I got an error message: 现在,每当我要删除表"computer"
使用的数据时,都会收到错误消息:
A Database Error Occurred Error Number: 1451 发生数据库错误,错误号:1451
Cannot delete or update a parent row: a foreign key constraint fails (
cmms
.computer
, CONSTRAINTcomputer_ibfk_1
FOREIGN KEY (location
) REFERENCESlocation
(location_id
)) 无法删除或更新父行,外键约束失败(cmms
。computer
,约束computer_ibfk_1
外键(location
)参考location
(location_id
))DELETE FROM
location
WHERElocation_id
= '30' 从location
WHERElocation_id
='30'删除Filename: C:/xampp/htdocs/cmms/application/models/asset_model.php 文件名:C:/xampp/htdocs/cmms/application/models/asset_model.php
Line Number: 9 行号:9
How can I check if it exist in table "computer" there will be a message and if it doesn't exist it will automatically delete the data in "location" table? 如何检查“计算机”表中是否存在该消息,如果不存在,它将自动删除“位置”表中的数据?
my code in my controller which is asset_management.php
is: 我在控制器中的代码为asset_management.php
:
public function deleteloc(){
$this->load->model("asset_model");
$u = $this->uri->segment(3);
$this->asset_model->deletelocation($u);
redirect('asset_management/locations');
}
My code in my model which is asset_model is: 我在模型中的代码为asset_model:
public function deletelocation($a){
$this->db->delete('location', array('location_id' => $a));
return;
}
please help. 请帮忙。 THANK YOU SO MUCH!! 非常感谢!!
You need to remove a value from the computer table before being able to remove this row. 您需要先从计算机表中删除一个值,然后才能删除该行。 There is a row in the computer table depending on this row. 计算机表中有一行取决于该行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.