This is another article written in order to handle error which is generated from MySQL Database Server. The error as specified in the title is ‘Cannot add or update a child row: a foreign key constraint fails’. The error itself is triggered when a query is being executed. In the context of this article, the query which is being executed is performed to add a record in the table.

The scenario is actually similar or in other words it is the scenario which is used in other article titled ‘MySQL Database generate ERROR 1215 (HY000): Cannot add foreign key constraint‘ which can be visited in this link.

The relationship between tables can be viewed in the following image :
MySQL Database generate ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails

Basically, the scenario is following the above ERD (Entity Relationship Diagram) with the availability of three tables. The error triggered when a query which is performed to insert a new record is being executed in table user_role. Below is the example of the query executed through MySQL Command Console :