MySQL 8.4Lab-verified · 2026-11-30

MySQL ERROR 1822 (HY000): Failed to add the foreign key constraint. Missing index

The column the foreign key points to has no index in the parent table that starts with that column.

MySQL 8.4.13
1822 (HY000): Failed to add the foreign key constraint. Missing index for constraint 'emp_ibfk_1' in the referenced table 'dept'

Jump to a cause

  1. 01The referenced parent column has no index
  2. 02The FK points at the second column of a composite primary key

Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-11-30 · container image mysql:8.4

ERROR 1822 is the MySQL 8.0 style error for a foreign key whose referenced column in the parent table has no usable index. In MySQL 8.4 the default setting restrict_fk_on_non_standard_key = ON makes the same CREATE TABLE fail with ERROR 6125 (Missing unique key) instead. To show the 8.0 behaviour, each reproduction below first runs SET SESSION restrict_fk_on_non_standard_key = OFF; (it returned 1 warning).

The message names the constraint (emp_ibfk_1) and the parent table. Check that table's indexes in information_schema.statistics. The fixes verified here are a UNIQUE key on the parent column or referencing the whole primary key, and the fixed statements ran with the 8.4 default (no SET).

01The referenced parent column has no index

dept had a primary key on dept_id only. Creating emp with FOREIGN KEY (dept_code) REFERENCES dept (dept_code) failed with Missing index for constraint 'emp_ibfk_1' in the referenced table 'dept'. information_schema.statistics showed only PRIMARY on dept_id.

After ALTER TABLE dept ADD UNIQUE KEY uk_dept_code (dept_code); the same CREATE TABLE worked, and employee 100 with D10 joined to Sales.

Setup
Setuprun as lab_a
CREATE TABLE dept (dept_id INT PRIMARY KEY, dept_code VARCHAR(10), name VARCHAR(50));
INSERT INTO dept VALUES (1, 'D10', 'Sales'), (2, 'D20', 'IT');
OUTPUT
Query OK, 0 rows affected (0.01 sec)

Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
SET SESSION restrict_fk_on_non_standard_key = OFF;
CREATE TABLE emp (emp_id INT PRIMARY KEY, dept_code VARCHAR(10), FOREIGN KEY (dept_code) REFERENCES dept (dept_code));
✕ ERROR
Query OK, 0 rows affected, 1 warning (0.00 sec)

ERROR 1822 (HY000): Failed to add the foreign key constraint. Missing index for constraint 'emp_ibfk_1' in the referenced table 'dept'
Checkrun as lab_a
SELECT index_name, column_name, non_unique FROM information_schema.statistics WHERE table_schema = 'lab_a' AND table_name = 'dept';
OUTPUT
+------------+-------------+------------+
| INDEX_NAME | COLUMN_NAME | NON_UNIQUE |
+------------+-------------+------------+
| PRIMARY    | dept_id     |          0 |
+------------+-------------+------------+
1 row in set (0.00 sec)
Fixrun as lab_a
ALTER TABLE dept ADD UNIQUE KEY uk_dept_code (dept_code);
OUTPUT
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

Use a UNIQUE key, not a plain index, on the parent column. MySQL 8.4 by default accepts only a unique key here.

After the fixrun as lab_a
CREATE TABLE emp (emp_id INT PRIMARY KEY, dept_code VARCHAR(10), FOREIGN KEY (dept_code) REFERENCES dept (dept_code));
INSERT INTO emp VALUES (100, 'D10');
SELECT e.emp_id, d.name FROM emp e JOIN dept d ON d.dept_code = e.dept_code;
✓ SUCCESS
Query OK, 0 rows affected (0.01 sec)

Query OK, 1 row affected (0.00 sec)

+--------+-------+
| emp_id | name  |
+--------+-------+
|    100 | Sales |
+--------+-------+
1 row in set (0.00 sec)

02The FK points at the second column of a composite primary key

order_line has PRIMARY KEY (order_id, line_no). shipment referenced only line_no and failed with Missing index for constraint 'shipment_ibfk_1'. The index exists, but line_no is its second column (SEQ_IN_INDEX 2), and a single line_no is not unique anyway: order 1 has lines 1 and 2.

Referencing the whole key, FOREIGN KEY (order_id, line_no) REFERENCES order_line (order_id, line_no), worked, and shipment 10 joined to ink (order 1, line 2).

Setup
Setuprun as lab_a
CREATE TABLE order_line (order_id INT, line_no INT, item VARCHAR(20), PRIMARY KEY (order_id, line_no));
INSERT INTO order_line VALUES (1, 1, 'pen'), (1, 2, 'ink');
OUTPUT
Query OK, 0 rows affected (0.01 sec)

Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
SET SESSION restrict_fk_on_non_standard_key = OFF;
CREATE TABLE shipment (ship_id INT PRIMARY KEY, order_id INT, line_no INT, FOREIGN KEY (line_no) REFERENCES order_line (line_no));
✕ ERROR
Query OK, 0 rows affected, 1 warning (0.00 sec)

ERROR 1822 (HY000): Failed to add the foreign key constraint. Missing index for constraint 'shipment_ibfk_1' in the referenced table 'order_line'
Checkrun as lab_a
SELECT index_name, column_name, seq_in_index FROM information_schema.statistics WHERE table_schema = 'lab_a' AND table_name = 'order_line' ORDER BY seq_in_index;
OUTPUT
+------------+-------------+--------------+
| INDEX_NAME | COLUMN_NAME | SEQ_IN_INDEX |
+------------+-------------+--------------+
| PRIMARY    | order_id    |            1 |
| PRIMARY    | line_no     |            2 |
+------------+-------------+--------------+
2 rows in set (0.00 sec)

When the parent key has several columns, the child table needs the same columns in the same order.

After the fixrun as lab_a
CREATE TABLE shipment (ship_id INT PRIMARY KEY, order_id INT, line_no INT, FOREIGN KEY (order_id, line_no) REFERENCES order_line (order_id, line_no));
INSERT INTO shipment VALUES (10, 1, 2);
SELECT s.ship_id, l.item FROM shipment s JOIN order_line l ON l.order_id = s.order_id AND l.line_no = s.line_no;
✓ SUCCESS
Query OK, 0 rows affected (0.02 sec)

Query OK, 1 row affected (0.00 sec)

+---------+------+
| ship_id | item |
+---------+------+
|      10 | ink  |
+---------+------+
1 row in set (0.00 sec)

FAQ

What does MySQL ERROR 1822 Missing index for constraint mean?

The parent column in REFERENCES has no index that starts with it. In the lab the parent column had no index at all, and in the other case it was the second column of a composite key.

Why do I get 6125 instead of 1822 on MySQL 8.4?

MySQL 8.4 has restrict_fk_on_non_standard_key = ON by default and requires a unique key, so it reports ERROR 6125 Missing unique key. With the session setting OFF, the lab got 1822 as in 8.0.

How do I fix ERROR 1822?

Add a UNIQUE key on the parent column (ALTER TABLE dept ADD UNIQUE KEY uk_dept_code (dept_code);) or reference all columns of the parent's primary key. Both worked in the lab.

Can a foreign key reference part of a composite primary key?

In the lab, referencing only line_no from PRIMARY KEY (order_id, line_no) failed with 1822. Referencing (order_id, line_no) worked.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary users created for the test, each with full rights on a database of the same name (its default database); root is the administrator. MySQL 8.4.13