Answer the question
In order to leave comments, you need to log in
Can Mysql error code be trusted?
Hello! If the posts table has a connection with the users table through the user_id => id field and an insertion occurs into the posts table where the user_id field contains the value 3, but there is no such id in the users table. Then Mysql returns an error with the number 1452.
Can I trust this number and be sure that it will not change in the same situation later and make a check
if($db->errno === 1452){
throw new Exception("Такого пользователя не существует");
}
Answer the question
In order to leave comments, you need to log in
Server Error Message Reference
Error number: 1452; Symbol: ER_NO_REFERENCED_ROW_2; SQLSTATE: 23000
Message: Cannot add or update a child row: a foreign key constraint fails (%s)
InnoDB reports this error when you try to add a row but there is no parent row, and a foreign key constraint fails. Add the parent row first.
Or, before inserting, you need to create a query to check the existence of such a record in the link table?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question