4
I am not able to treat an exception that happens when I delete an address that is being used in another table. I’m using the codeigniter
my code is:
public function delete_endereco($cod_clientes_endereco){
try{
$acao = $this->db->delete('clientes_enderecos', array('cod_clientes_endereco' => $cod_clientes_endereco));
}
catch (SqlException $ex) {
SqlException::throwDeleteException($ex);
}
if($acao){
$this->session->set_flashdata('success', 'Endereço deletado com sucesso!');
}
else{
$this->session->set_flashdata('error', 'Este endereço não pode ser deletado, pois existe um pedido utilizando ele.');
}
redirect("pagina_cliente?page=enderecos");
}
The following error keeps appearing:
A Database Error Occurred
Error Number: 1451
Cannot delete or update a parent row: a foreign key constraint fails (`db`.`pedidos`, CONSTRAINT `fk_pedidos_clientes_enderecos1` FOREIGN KEY (`endereco_cobranca`) REFERENCES `clientes_enderecos` (`cod_clientes_endereco`) ON DELETE NO ACTION ON UPDATE NO ACTIO)
DELETE FROM `clientes_enderecos` WHERE `cod_clientes_endereco` = '5'
Filename: controllers/Pagina_cliente.php
Line Number: 160
Can’t mean what exactly?
– rray
@rray instead of capturing the exception so that I can handle by displaying an error msg, appears: Cannot delete or update a Parent Row: a Foreign key Constraint fails
– guilherme
Error and php Exception are two different things, in this case just remove the Try-catch, let the if solve this.
– rray
@rray already tried to remove the Try-catch, but keeps showing the same screen with the error.
– guilherme
This type error appear has no problem in approval environment, in the production environment they will be hidden, of course you should treat and log them.
– rray