MySQL Error 6125: Missing Unique Parent Key
Fix MySQL Error 6125 by referencing a complete PRIMARY or UNIQUE key, or adding a justified unique index to the parent table.
On this page
MySQL Error 6125 (HY000, ER_FK_NO_UNIQUE_INDEX_PARENT) means a foreign key references parent columns that do not form a complete unique key. MySQL 8.4 and 9.7 reject non-unique or partial parent keys by default. See the MySQL 8.4 error reference and foreign-key requirements.
Check the referenced parent key
Inspect the live table definition and its indexes:
SHOW CREATE TABLE parent\G
SHOW INDEX FROM parent;
The referenced columns must match all columns of a PRIMARY KEY or UNIQUE key in the same order. A leftmost prefix of a longer composite unique key is only a partial key. For example, if the parent has UNIQUE (a, b), a foreign key that references only a is not a complete unique-key reference:
CREATE TABLE parent (
a INT NOT NULL,
b INT NOT NULL,
UNIQUE KEY uq_parent_a_b (a, b)
) ENGINE = InnoDB;
CREATE TABLE child (
id INT PRIMARY KEY,
parent_a INT NOT NULL,
CONSTRAINT fk_child_parent
FOREIGN KEY (parent_a) REFERENCES parent (a)
) ENGINE = InnoDB;
Before changing an index, check whether the proposed single column is actually unique:
SELECT a, COUNT(*) AS row_count
FROM parent
WHERE a IS NOT NULL
GROUP BY a
HAVING COUNT(*) > 1;
The local MySQL foreign-key checker can compare common type, engine, and parent-key conditions without sending schema values to a server. It does not inspect your live indexes, so verify the actual parent definition with SHOW CREATE TABLE or SHOW INDEX.
Choose a key that matches the data model
If a is meant to identify one parent row, check for duplicate non-NULL values, then create a unique index. A NULL child key does not require a matching parent row, and MySQL unique indexes permit multiple NULL values:
CREATE UNIQUE INDEX uq_parent_a ON parent (a);
If the parent row is identified by the pair (a, b), include both columns in the child foreign key and reference them in the same order:
CREATE TABLE child (
id INT PRIMARY KEY,
parent_a INT NOT NULL,
parent_b INT NOT NULL,
CONSTRAINT fk_child_parent
FOREIGN KEY (parent_a, parent_b)
REFERENCES parent (a, b)
) ENGINE = InnoDB;
For an existing child table, populate and verify the additional key value for every row before adding the composite constraint. Do not add a unique index unless the data model requires those values to be unique; an index should enforce the intended relationship, not just suppress an error.
Do not rely on non-standard parent keys
Check restrict_fk_on_non_standard_key only when diagnosing a legacy schema:
SHOW VARIABLES LIKE 'restrict_fk_on_non_standard_key';
In MySQL 8.4 and 9.7, the default is ON. Setting it to OFF permits deprecated foreign keys that reference non-unique or partial parent keys, but MySQL warns that support may be removed. Prefer changing the schema to use a complete unique parent key. See the system-variable reference.
Distinguish Error 6125 from related errors
- Error 6125: a non-unique or partial parent key is not allowed under the current settings.
- Error 1822: the referenced columns do not have a suitable parent index. See Error 1822 troubleshooting.
- Error 3780: corresponding child and parent columns have incompatible definitions. See Error 3780 troubleshooting.
- Error 1215: a generic foreign-key definition error used by older versions or other failure cases. See Error 1215 troubleshooting.
For other constraint rules, see the MySQL foreign-key guide.