MySQL error 1824: Failed to open the referenced table
A foreign key refers to a parent table that doesn't exist, often because the tables are created in the wrong order.
- Error number
1824- Name
ER_FK_CANNOT_OPEN_PARENT- SQLSTATE
HY000- Category
- Keys and constraints
What it means
When creating a foreign key, InnoDB opens the referenced table to check it. If that table doesn't exist (or is named in another database or case), creation fails with 1824.
Common causes
- Creating the child table before the parent in a schema script.
- A typo or wrong database in
REFERENCES. - Letter-case differences on a case-sensitive server.
How to fix it
-
Create the parent table first, or add foreign keys at the end of the script with
ALTER TABLE. -
When a script must create tables in any order, disable checks while creating them.
SET foreign_key_checks = 0; -- CREATE TABLE statements SET foreign_key_checks = 1;
Related errors
More keys and constraints errors
1062Duplicate entry for key1061Duplicate key name1452Foreign key fails: cannot add child row1451Foreign key fails: cannot delete parent row1216Cannot add or update a child row1217Cannot delete or update a parent row3780Foreign key columns are incompatible1822Missing index for foreign key constraint
The code and message text are from the MySQL 8.4 Error Message Reference. Browse the full MySQL error list or search every database error.
Work on MySQL in SQLly
A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for MySQL and the other databases you use.