如何在CodeIgniter,SQLServer中处理外键约束失败

时间:2019-07-14 02:24:31

标签: sql-server codeigniter

我正在Codeigniter中创建新的应用程序,后端是Ms-SQL Server。我在表之间添加了一些外键。现在,当我尝试从主表中删除记录时,它给了我类似[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]The DELETE statement conflicted with the REFERENCE constraint "FK_roles_mst_role_level_mst1的错误,这非常明显。现在,我想处理此错误,并向用户显示确切的问题是什么,以及用户需要从辅助表中删除多少记录才能从主表中删除记录。 下面是我的表结构

Primary table(role_level_mst):

ID - Pk
RoleLevelName

Secondary Table(Role_mst):
ID - PK
fk_role_level(FK with role_level_mst)
role_name```

I have tried with try catch which is not working. Than also tried to disable db_debug but it disables all the errors.

if (!$this->db->delete($table,$whereData)) {
  // if query returns null
  $msg = $this->db->_error_message();
  $num = $this->db->_error_number();

  $data['msg'] = "Error(".$num.") ".$msg; 
}

0 个答案:

没有答案