MySQL 1205: Error in foreign key constraint.

  1. Identify the Query and Table Causing the Error:


- To identify the problematic query and table, review your application's log files or enable MySQL's general log temporarily with:
SET global general_log = 1;
SET global log_output = 'table';

After replicating the error, disable it with `SET global general_log = 0;` to avoid performance issues. Then, query the log:
SELECT argument FROM mysql.general_log WHERE argument LIKE '%FOREIGN KEY%';

  1. Check for Inconsistencies in Schema Definitions:


- Ensure both tables involved in the foreign key constraint have the same column definitions (data type, length, etc.) for the foreign key and referenced key. Use the `DESCRIBE table_name;` command for both tables to compare.

  1. Verify Data Types and Values:


- Ensure the foreign key column in the child table matches the corresponding primary key in the parent table in terms of data type and attributes. For instance, if one is `UNSIGNED` and the other is not, it could cause issues.

  1. Check for Orphaned Rows:


- Check if there are any rows in the child table that do not have corresponding entries in the parent table. Run a query like:
SELECT * FROM childtable LEFT JOIN parenttable ON childtable.foreignkey = parenttable.primarykey WHERE parenttable.primarykey IS NULL;

  1. Examine Locks:


- The error might be due to a lock. To check for locks, use:
SHOW ENGINE INNODB STATUS;
Look under the `LATEST FOREIGN KEY ERROR` section for details about the error and under `TRANSACTIONS` for any locked tables.

  1. Correct or Remove the Erroneous Foreign Key Constraint:


- If a discrepancy is found or a decision is made to remove the constraint, first identify the constraint name with:
SELECT CONSTRAINTNAME FROM INFORMATIONSCHEMA.KEYCOLUMNUSAGE WHERE TABLENAME = 'yourtable_name';
- Then, drop the constraint (if decided):
ALTER TABLE yourtablename DROP FOREIGN KEY constraint_name;
- Or, correct the data causing the issue based on findings from steps 1-4.

  1. If a Constraint Needs to Be Re-added or Created Anew:


- After correcting any issues, you can re-add or add a new constraint with:
ALTER TABLE childtable ADD CONSTRAINT fkname FOREIGN KEY (foreignkeycolumn) REFERENCES parenttable(parentkey_column);

Remember to replace `yourtablename`, `childtable`, `parenttable`, `foreignkeycolumn`, `parentkeycolumn`, and `fk_name` with your actual table and column names.

Master

MySQL

in Minutes — Grab the Ultimate Cheatsheet

(Perfect for DevOps & SREs)

Most-used commands
Real-world configs/examples
Handy troubleshooting shortcuts
Your email is safe with us. No spam, ever.

Thankyou for your submission

We have sent the cheatsheet on your email!
Oops! Something went wrong while submitting the form.

MySQL

Cheatsheet

(Perfect for DevOps & SREs)

Most-used commands
Your email is safe with us. No spam, ever.

Thankyou for your submission

We have sent the cheatsheet on your email!
Oops! Something went wrong while submitting the form.

MORE ISSUES

Made with ❤️ in Bangalore & San Francisco 🏢

Doctor Droid