Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-11-29 · container image mysql:8.4
6125 appears when you create a foreign key whose referenced columns are not exactly a PRIMARY KEY or UNIQUE key of the parent table. The message names the constraint and the parent table, for example 'orders_ibfk_1' in the referenced table 'customers'.
In MySQL 8.4 a plain (non-unique) index on the parent column is not enough: the test below fails even with INDEX ix_products_sku (sku). The error is in English in every language.
01The referenced column has no UNIQUE key
orders.customer_email points to customers.email, but customers only has its PRIMARY key on id. The index check shows that single row.
Setup
CREATE TABLE customers (id INT PRIMARY KEY, email VARCHAR(100));Query OK, 0 rows affected (0.01 sec)CREATE TABLE orders (id INT PRIMARY KEY, customer_email VARCHAR(100), FOREIGN KEY (customer_email) REFERENCES customers (email));ERROR 6125 (HY000): Failed to add the foreign key constraint. Missing unique key for constraint 'orders_ibfk_1' in the referenced table 'customers'SELECT index_name, column_name, non_unique FROM information_schema.statistics WHERE table_schema = 'lab_a' AND table_name = 'customers';+------------+-------------+------------+
| INDEX_NAME | COLUMN_NAME | NON_UNIQUE |
+------------+-------------+------------+
| PRIMARY | id | 0 |
+------------+-------------+------------+
1 row in set (0.00 sec)ALTER TABLE customers ADD UNIQUE KEY uk_customers_email (email);Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0If each email belongs to only one customer, add a unique key: ALTER TABLE customers ADD UNIQUE KEY uk_customers_email (email);. Then the same CREATE TABLE succeeds.
CREATE TABLE orders (id INT PRIMARY KEY, customer_email VARCHAR(100), FOREIGN KEY (customer_email) REFERENCES customers (email));Query OK, 0 rows affected (0.01 sec)02The FK references only part of a composite primary key
order_lines has PRIMARY KEY (order_id, line_no). line_no alone is not unique (every order has line 1), so a foreign key on line_no only is rejected. The check shows line_no is the second column of PRIMARY.
Setup
CREATE TABLE order_lines (order_id INT, line_no INT, item VARCHAR(50), PRIMARY KEY (order_id, line_no));Query OK, 0 rows affected (0.02 sec)CREATE TABLE shipments (id INT PRIMARY KEY, line_no INT, CONSTRAINT fk_ship_line FOREIGN KEY (line_no) REFERENCES order_lines (line_no));ERROR 6125 (HY000): Failed to add the foreign key constraint. Missing unique key for constraint 'fk_ship_line' in the referenced table 'order_lines'SELECT index_name, seq_in_index, column_name FROM information_schema.statistics WHERE table_schema = 'lab_a' AND table_name = 'order_lines' ORDER BY seq_in_index;+------------+--------------+-------------+
| INDEX_NAME | SEQ_IN_INDEX | COLUMN_NAME |
+------------+--------------+-------------+
| PRIMARY | 1 | order_id |
| PRIMARY | 2 | line_no |
+------------+--------------+-------------+
2 rows in set (0.00 sec)Reference the whole key: add order_id to the child table and declare FOREIGN KEY (order_id, line_no) REFERENCES order_lines (order_id, line_no). That CREATE TABLE succeeds.
CREATE TABLE shipments (id INT PRIMARY KEY, order_id INT, line_no INT, CONSTRAINT fk_ship_line FOREIGN KEY (order_id, line_no) REFERENCES order_lines (order_id, line_no));Query OK, 0 rows affected (0.02 sec)03The parent column has an index, but not a UNIQUE one
products.sku has INDEX ix_products_sku (sku), a normal non-unique index. MySQL 8.4 still refuses the foreign key with 6125.
Setup
CREATE TABLE products (id INT PRIMARY KEY, sku VARCHAR(20), INDEX ix_products_sku (sku));
INSERT INTO products VALUES (1, 'A-100'), (2, 'B-200');Query OK, 0 rows affected (0.02 sec)
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0CREATE TABLE stock (id INT PRIMARY KEY, sku VARCHAR(20), FOREIGN KEY (sku) REFERENCES products (sku));ERROR 6125 (HY000): Failed to add the foreign key constraint. Missing unique key for constraint 'stock_ibfk_1' in the referenced table 'products'SELECT sku, COUNT(*) FROM products GROUP BY sku HAVING COUNT(*) > 1;Empty set (0.00 sec)ALTER TABLE products DROP INDEX ix_products_sku, ADD UNIQUE KEY uk_products_sku (sku);Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0First check that there are no duplicate SKUs (GROUP BY sku HAVING COUNT(*) > 1 returns Empty set). Then replace the index with a unique one in one statement: ALTER TABLE products DROP INDEX ix_products_sku, ADD UNIQUE KEY uk_products_sku (sku);. The FK is now accepted. If duplicates exist, clean them up before adding the unique key.
CREATE TABLE stock (id INT PRIMARY KEY, sku VARCHAR(20), FOREIGN KEY (sku) REFERENCES products (sku));Query OK, 0 rows affected (0.02 sec)FAQ
How do I fix Missing unique key for constraint in MySQL?
Make the referenced columns a PRIMARY KEY or UNIQUE key in the parent table, or point the foreign key at the parent's primary key instead.
Why does a normal INDEX on the parent column not work in MySQL 8.4?
In the MySQL 8.4 test, a foreign key to a column with only a non-unique INDEX failed with 6125. Changing it to a UNIQUE KEY made it work.
Can a foreign key reference one column of a composite primary key?
Not on its own. Reference all columns of the key in the same order, for example (order_id, line_no).
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