I'm implementing a code where you have a foreign key in two tables. I have already tried different media and can not handle the error:
Error Number: 1451
Cannot delete or update a parent row: a foreign key constraint fails ('base'.'tableus', CONSTRAINT 'doctor_ibfk_1' FOREIGN KEY ('department_id') REFERENCES 'department' ('department_id'))
DELETE FROM 'department' WHERE 'department_id' = '1'
Filename: models/Crud.php
Line Number: 292
I've used an example that I found Here and did not resolve it.
I'm reviewing my question here, let me pass on one important detail, I do not want the user to delete the data from the 'parent' table while having 'daughters' tables attached to it. Every time the user tries to do this he should get an error, in case I can not manage to handle this error in codeigniter.
function delete_department($department_id)
{
//verifica se recebe mensagem de erro na exclusão.
$verifica_erro = $this->db->_error_number() == 1451;
if ($this->delete_department($department_id) == $verifica_erro){
echo "Mensagem de erro para o usuário!";
}else{
//senão tiver executa a exclusão.
$this->db->where('department_id',$department_id);
$this->db->delete('department');
}
}
This code executes and after another error:
Fatal error: Uncaught TypeError: Argument 1 passed to CI_Exceptions :: show_exception () must be an instance of Exception, instance of Error given, called in /Path/system/core/Common.php on line 658 and defined in / Path / system / core / Exceptions.php: 190 Stack trace: # 0 /Path/system/core/Common.php(658): CI_Exceptions-> show_exception (Object (Error)) # 1 [internal function]: _exception_handler ( Object (Error)) # 2 {main} thrown in /Path/system/core/Exceptions.php online 190