Error
Error Code: 1821

MariaDB Error 1821: Failed Foreign Key Constraint

📦 MariaDB
📋

Description

Error 1821 indicates that MariaDB failed to add a foreign key constraint because an essential index is missing on the foreign key columns in the child table. This index is crucial for the database to efficiently enforce referential integrity and perform lookups.
💬

Error Message

Failed to add the foreign key constaint. Missing index for constraint '%s' in the foreign table '%s'
🔍

Known Causes

3 known causes
⚠️
Missing Index in Child Table
The columns designated as the foreign key in the child table do not have an associated index, which is mandatory for foreign key creation and efficient lookups.
⚠️
Mismatched Column Definitions
The data types, lengths, or collations of the foreign key columns in the child table do not precisely match the referenced primary/unique key columns in the parent table.
⚠️
Unsupported Storage Engine
The child table uses a storage engine (e.g., MyISAM) that does not support foreign key constraints, thus preventing the necessary index from being created or recognized.
🛠️

Solutions

3 solutions available

1. Add Index to Foreign Table easy

Create an index on the referencing column(s) in the foreign table.

1
Identify the foreign table and the column(s) involved in the foreign key constraint. The error message will typically provide this information, for example: `Missing index for constraint 'fk_user_id' in the foreign table 'users'` implies the foreign table is 'users' and the column is likely 'user_id'.
2
Connect to your MariaDB instance using a client like `mysql` or MariaDB Workbench.
mysql -u your_user -p your_database
3
Execute an `ALTER TABLE` statement to add an index to the foreign table on the column(s) that will be referenced by the foreign key.
ALTER TABLE foreign_table_name ADD INDEX index_name (column_name);
4
If the foreign key spans multiple columns, add a composite index.
ALTER TABLE foreign_table_name ADD INDEX index_name (column1, column2);
5
After adding the index, retry creating the foreign key constraint.
ALTER TABLE child_table ADD CONSTRAINT constraint_name FOREIGN KEY (fk_column) REFERENCES parent_table(pk_column);

2. Create Table with Index First easy

When creating tables, define the index before the foreign key constraint.

1
When defining your `CREATE TABLE` statements, ensure that any columns intended to be part of a foreign key relationship have an index defined on them in the referencing (child) table.
CREATE TABLE child_table (
    id INT AUTO_INCREMENT PRIMARY KEY,
    fk_column INT,
    -- other columns...
    INDEX fk_column_idx (fk_column),
    FOREIGN KEY (fk_column) REFERENCES parent_table(pk_column)
);
2
If the foreign key involves multiple columns, define a composite index.
CREATE TABLE child_table (
    id INT AUTO_INCREMENT PRIMARY KEY,
    fk_column1 INT,
    fk_column2 INT,
    -- other columns...
    INDEX composite_fk_idx (fk_column1, fk_column2),
    FOREIGN KEY (fk_column1, fk_column2) REFERENCES parent_table(pk_column1, pk_column2)
);

3. Recreate Table with Index medium

Drop and recreate the foreign table with the necessary index.

1
Back up the data in the foreign table. This is crucial to avoid data loss.
mysqldump -u your_user -p your_database foreign_table_name > foreign_table_backup.sql
2
Drop the foreign table.
DROP TABLE foreign_table_name;
3
Recreate the foreign table, ensuring the column(s) involved in the foreign key relationship have an index defined.
CREATE TABLE foreign_table_name (
    id INT AUTO_INCREMENT PRIMARY KEY,
    fk_column INT,
    -- other columns...
    INDEX fk_column_idx (fk_column)
);
4
Restore the data into the recreated foreign table.
mysql -u your_user -p your_database < foreign_table_backup.sql
5
Now, attempt to add the foreign key constraint again. It should succeed.
ALTER TABLE child_table ADD CONSTRAINT constraint_name FOREIGN KEY (fk_column) REFERENCES parent_table(pk_column);
🔗

Related Errors

5 related errors