MySQL 1220: Cannot update parent row.

When encountering the error 1220: Cannot update parent row in MySQL, the user should take the following immediate actions:

  1. Identify the Foreign Key Constraint Causing the Issue:


- Run the query to find foreign key constraints related to the table you are trying to update:
SELECT CONSTRAINTNAME, TABLENAME, COLUMNNAME, REFERENCEDTABLENAME, REFERENCEDCOLUMN_NAME
FROM INFORMATION
SCHEMA.KEYCOLUMN_USAGE
WHERE REFERENCED
TABLESCHEMA = 'yourdatabasename' AND REFERENCEDTABLENAME = 'parenttablename';
Replace `'yourdatabasename'` and `'parenttablename'` with your actual database name and the parent table's name you are trying to update.

  1. Check for Orphaned Rows:


- Identify if there are any orphaned rows in the child table that are preventing the update. Run a query like the following, adjusting it to match your table and column names:
SELECT child_table.*
FROM child_table
LEFT JOIN parent
table ON childtable.parentid = parenttable.id
WHERE parent_table.id IS NULL;

This query will help you find rows in the child table that have no corresponding parent row.

  1. Review the Specific Rows Causing the Error:


- If you know the specific row or condition that triggers the error, examine those rows directly:
SELECT * FROM parenttable WHERE somecondition;
Replace `some_condition` with the condition relevant to your situation.

  1. Check the Data Type and Values:


- Ensure that the data types of the foreign key columns match and that you are not trying to insert or update a value in the child table that does not exist in the parent table.

  1. Manual Update or Deletion (with Caution):


- If a specific row needs to be updated or deleted to resolve the issue, you can manually attempt to correct the data:
UPDATE parenttable SET columnname = 'new_value' WHERE condition;
Or, if it's necessary to delete:
DELETE FROM parent_table WHERE condition;
Note: Be very careful with manual updates or deletions, especially if you are not entirely sure of the impact. Always back up the relevant tables or the entire database before making such changes.

  1. Check for Triggers:


- Investigate if there are any triggers that might be automatically updating or inserting rows in the parent or child tables, which could be causing the issue:
SHOW TRIGGERS LIKE 'table_name%';
Replace `'table_name%'` with the name of the table you are concerned about.

Remember, these actions are provided for immediate investigation and potential resolution of the specific error message. Depending on the complexity of your database and the relationships between tables, some issues may require more in-depth analysis.

Never debug

MySQL

manually again

Let Dr. Droid create custom investigation plans for your infrastructure.

Book Demo
Automate Debugging for
MySQL
See how Dr. Droid creates investigation plans for your infrastructure.

MORE ISSUES

Made with ❤️ in Bangalore & San Francisco 🏢

Doctor Droid