// MySQL error reference

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
Error message
Failed to open the referenced table '%s'

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

  1. Create the parent table first, or add foreign keys at the end of the script with ALTER TABLE.

  2. 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

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.

// fewer errors, faster fixes

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.