php – Como tratar exceção lançada “Integrity constraint violation: Cannot delete or update a parent row: a foreign key constraint fails”?


I use the PDO class to communicate with the banco de dados and I don't know a good way to catch this exceção .

I PDOException a class that extends PDOException but I couldn't find it.

I can do a query before deleting, that works, but would you like to handle when the exception is thrown, or is this not the best way?


There are cases where the getCode() does not return the correct code.

In this case I use:

try {
} catch (PDOException $e) {
   if (isset($e->errorInfo[1]) && $e->errorInfo[1] == '1451') {
      print 'mensagem';
Scroll to Top