MySQL error 1826: Duplicate foreign key constraint name
A foreign key's name is already used. In MySQL foreign key names must be unique across the whole database, not just the table.
- Error number
1826- Name
ER_FK_DUP_NAME- SQLSTATE
HY000- Category
- Keys and constraints
What it means
InnoDB stores foreign key constraint names per database, so two tables can't each have a constraint called fk_customer. Creating the second one fails with 1826.
Common causes
- Copying a table definition, including its constraint names, to create a similar table.
- Naming conventions that don't include the table name.
How to fix it
-
Find where the name is used.
SELECT table_name, constraint_name FROM information_schema.referential_constraints WHERE constraint_schema = DATABASE() AND constraint_name = 'fk_customer';
-
Use names that include the table, such as
fk_orders_customerandfk_invoices_customer, or leave the name out and let MySQL generate one.
Related errors
More keys and constraints errors
1062Duplicate entry for key1452Foreign 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 constraint1824Failed to open the referenced table
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.