MySQL 8.4Lab-verified · 2026-12-01

MySQL ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails

The child row points to a parent key value that does not exist in the referenced table.

MySQL 8.4.13
1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`lab_a`.`orders`, CONSTRAINT `fk_orders_customer` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`))

Jump to a cause

  1. 01INSERT into the child table with a parent id that does not exist
  2. 02Adding a foreign key to a table that already has orphan rows
  3. 03Loading child data before parent data (restore or migration order)
  4. 040 used to mean "no parent" (old data or app default)
  5. 05UPDATE of a child row to a parent that does not exist
  6. 06A code with a trailing space from imported data ('JP ')

Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-12-01 · container image mysql:8.4

The message names the child table, the constraint, the FK column and the parent table: (lab_a.orders, CONSTRAINT fk_orders_customer FOREIGN KEY (customer_id) REFERENCES customers (id)). Take the value you tried to write into that column and look for it in the parent table — it is not there.

MySQL 8.4 returns this message in English even when lc_messages is set to Korean or Japanese.

01INSERT into the child table with a parent id that does not exist

orders.customer_id references customers.id. Only customer 1 exists, so inserting an order for customer 2 fails. Selecting customer 2 returns Empty set.

Setup
Setuprun as lab_a
CREATE TABLE customers (id INT PRIMARY KEY, name VARCHAR(50));
CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, CONSTRAINT fk_orders_customer FOREIGN KEY (customer_id) REFERENCES customers (id));
INSERT INTO customers VALUES (1, 'Kim');
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 1 row affected (0.00 sec)
Reproducerun as lab_a
INSERT INTO orders VALUES (100, 2);
✕ ERROR
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`lab_a`.`orders`, CONSTRAINT `fk_orders_customer` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`))
Checkrun as lab_a
SELECT id, name FROM customers WHERE id = 2;
OUTPUT
Empty set (0.00 sec)
Fixrun as lab_a
INSERT INTO customers VALUES (2, 'Lee');
OUTPUT
Query OK, 1 row affected (0.01 sec)

Insert the parent row first (customer 2), then the same order INSERT succeeds. If the id itself is wrong, fix the value you are inserting instead.

After the fixrun as lab_a
INSERT INTO orders VALUES (100, 2);
✓ SUCCESS
Query OK, 1 row affected (0.00 sec)

02Adding a foreign key to a table that already has orphan rows

ALTER TABLE … ADD CONSTRAINT … FOREIGN KEY checks the existing rows. Employee 3 has dept_id 30, which is not in depts. The table name in the message is a temporary name like #sql-1_107 because MySQL builds a copy of the table during the ALTER — the real table is the one in your ALTER.

Setup
Setuprun as lab_a
CREATE TABLE depts (id INT PRIMARY KEY, name VARCHAR(50));
CREATE TABLE emps (id INT PRIMARY KEY, dept_id INT);
INSERT INTO depts VALUES (10, 'Sales'), (20, 'IT');
INSERT INTO emps VALUES (1, 10), (2, 20), (3, 30);
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
ALTER TABLE emps ADD CONSTRAINT fk_emps_dept FOREIGN KEY (dept_id) REFERENCES depts (id);
✕ ERROR
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`lab_a`.`#sql-1_5b`, CONSTRAINT `fk_emps_dept` FOREIGN KEY (`dept_id`) REFERENCES `depts` (`id`))
Checkrun as lab_a
SELECT e.id, e.dept_id FROM emps e LEFT JOIN depts d ON d.id = e.dept_id WHERE e.dept_id IS NOT NULL AND d.id IS NULL;
OUTPUT
+----+---------+
| id | dept_id |
+----+---------+
|  3 |      30 |
+----+---------+
1 row in set (0.00 sec)
Fixrun as lab_a
UPDATE emps SET dept_id = NULL WHERE id = 3;
OUTPUT
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

Find orphan rows with a LEFT JOIN … WHERE parent.id IS NULL, then fix them (here dept_id set to NULL; NULL is allowed in an FK column). The ALTER then succeeds and copies the 3 rows.

After the fixrun as lab_a
ALTER TABLE emps ADD CONSTRAINT fk_emps_dept FOREIGN KEY (dept_id) REFERENCES depts (id);
✓ SUCCESS
Query OK, 3 rows affected (0.04 sec)
Records: 3  Duplicates: 0  Warnings: 0

03Loading child data before parent data (restore or migration order)

When you load data table by table, the products may arrive before the categories. categories is still empty (COUNT(*) is 0), so product 1 with category_id 5 is rejected.

Setup
Setuprun as lab_a
CREATE TABLE categories (id INT PRIMARY KEY, name VARCHAR(50));
CREATE TABLE products (id INT PRIMARY KEY, category_id INT, FOREIGN KEY (category_id) REFERENCES categories (id));
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.01 sec)
Reproducerun as lab_a
INSERT INTO products VALUES (1, 5);
✕ ERROR
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`lab_a`.`products`, CONSTRAINT `products_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`))
Check the causerun as lab_a
SELECT constraint_name, table_name, referenced_table_name FROM information_schema.referential_constraints WHERE constraint_schema = DATABASE();
OUTPUT
+-----------------+------------+-----------------------+
| CONSTRAINT_NAME | TABLE_NAME | REFERENCED_TABLE_NAME |
+-----------------+------------+-----------------------+
| products_ibfk_1 | products   | categories            |
+-----------------+------------+-----------------------+
1 row in set (0.00 sec)

products has a foreign key products_ibfk_1 (an auto-generated name, since none was given) that references categories. Every category_id you insert must already exist there, so the parent table has to be loaded first.

Checkrun as lab_a
SELECT COUNT(*) FROM categories;
OUTPUT
+----------+
| COUNT(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

Load parent tables first. If you cannot change the order, turn checks off for that session with SET FOREIGN_KEY_CHECKS = 0, load both tables, then set it back to 1. MySQL does not re-check rows loaded while it was off, so make sure every parent row is actually loaded — here the join shows product 1 with Stationery.

After the fixrun as lab_a
SET FOREIGN_KEY_CHECKS = 0;
INSERT INTO products VALUES (1, 5);
INSERT INTO categories VALUES (5, 'Stationery');
SET FOREIGN_KEY_CHECKS = 1;
SELECT p.id, c.name FROM products p JOIN categories c ON c.id = p.category_id;
✓ SUCCESS
Query OK, 0 rows affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

+----+------------+
| id | name       |
+----+------------+
|  1 | Stationery |
+----+------------+
1 row in set (0.00 sec)

040 used to mean "no parent" (old data or app default)

Old systems often store 0 instead of NULL when there is no referrer. Copying old_members into members fails, because there is no member with id 0.

NULL is not checked by a foreign key. After changing 0 to NULL in the old data (Rows matched: 2 Changed: 2), the same INSERT ... SELECT copies all 3 rows, and Kim and Park get referrer_id NULL.

Setup
Setuprun as lab_a
CREATE TABLE members (id INT PRIMARY KEY, name VARCHAR(50), referrer_id INT NULL, CONSTRAINT fk_referrer FOREIGN KEY (referrer_id) REFERENCES members (id));
CREATE TABLE old_members (id INT, name VARCHAR(50), referrer_id INT DEFAULT 0);
INSERT INTO old_members VALUES (1, 'Kim', 0), (2, 'Lee', 1), (3, 'Park', 0);
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
INSERT INTO members SELECT id, name, referrer_id FROM old_members ORDER BY id;
✕ ERROR
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`lab_a`.`members`, CONSTRAINT `fk_referrer` FOREIGN KEY (`referrer_id`) REFERENCES `members` (`id`))
Check the causerun as lab_a
SELECT referrer_id, COUNT(*) AS cnt FROM old_members GROUP BY referrer_id ORDER BY referrer_id;
SELECT COLUMN_NAME, IS_NULLABLE FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'lab_a' AND TABLE_NAME = 'members' AND COLUMN_NAME = 'referrer_id';
OUTPUT
+-------------+-----+
| referrer_id | cnt |
+-------------+-----+
|           0 |   2 |
|           1 |   1 |
+-------------+-----+
2 rows in set (0.00 sec)

+-------------+-------------+
| COLUMN_NAME | IS_NULLABLE |
+-------------+-------------+
| referrer_id | YES         |
+-------------+-------------+
1 row in set (0.00 sec)

Two rows have referrer_id = 0, and the column in members is nullable (IS_NULLABLE = YES).

Fixrun as lab_a
UPDATE old_members SET referrer_id = NULL WHERE referrer_id = 0;
OUTPUT
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2  Changed: 2  Warnings: 0
After the fixrun as lab_a
INSERT INTO members SELECT id, name, referrer_id FROM old_members ORDER BY id;
SELECT id, name, referrer_id FROM members ORDER BY id;
✓ SUCCESS
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

+----+------+-------------+
| id | name | referrer_id |
+----+------+-------------+
|  1 | Kim  |        NULL |
|  2 | Lee  |           1 |
|  3 | Park |        NULL |
+----+------+-------------+
3 rows in set (0.00 sec)

05UPDATE of a child row to a parent that does not exist

1452 is not only for INSERT. Moving employee 2 to department 30 fails because depts has only 10 and 20. The message says Cannot add or update a child row either way.

After adding department 30 (Marketing), the same UPDATE works and Lee shows up in Marketing.

Setup
Setuprun as lab_a
CREATE TABLE depts (id INT PRIMARY KEY, name VARCHAR(50));
CREATE TABLE emps (id INT PRIMARY KEY, name VARCHAR(50), dept_id INT, CONSTRAINT fk_emps_dept FOREIGN KEY (dept_id) REFERENCES depts (id));
INSERT INTO depts VALUES (10, 'Sales'), (20, 'IT');
INSERT INTO emps VALUES (1, 'Kim', 10), (2, 'Lee', 10);
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
UPDATE emps SET dept_id = 30 WHERE id = 2;
✕ ERROR
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`lab_a`.`emps`, CONSTRAINT `fk_emps_dept` FOREIGN KEY (`dept_id`) REFERENCES `depts` (`id`))
Check the causerun as lab_a
SELECT id, name FROM depts ORDER BY id;
OUTPUT
+----+-------+
| id | name  |
+----+-------+
| 10 | Sales |
| 20 | IT    |
+----+-------+
2 rows in set (0.00 sec)

depts has only 10 (Sales) and 20 (IT): department 30 does not exist.

Fixrun as lab_a
INSERT INTO depts VALUES (30, 'Marketing');
OUTPUT
Query OK, 1 row affected (0.01 sec)
After the fixrun as lab_a
UPDATE emps SET dept_id = 30 WHERE id = 2;
SELECT e.id, e.name, d.name AS dept FROM emps e JOIN depts d ON d.id = e.dept_id ORDER BY e.id;
✓ SUCCESS
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

+----+------+-----------+
| id | name | dept      |
+----+------+-----------+
|  1 | Kim  | Sales     |
|  2 | Lee  | Marketing |
+----+------+-----------+
2 rows in set (0.00 sec)

06A code with a trailing space from imported data ('JP ')

Copying from an import table fails, but the code looks like JP. The value actually has a trailing space, so 'JP ' does not match the parent key JP. Note that the whole INSERT ... SELECT is rolled back, so even the valid row (Kim) is not inserted.

After TRIM() on the import data (Changed: 1), both rows are inserted.

Setup
Setuprun as lab_a
CREATE TABLE countries (code VARCHAR(5) PRIMARY KEY, name VARCHAR(50));
CREATE TABLE customers (id INT PRIMARY KEY, name VARCHAR(50), country_code VARCHAR(5), CONSTRAINT fk_cust_country FOREIGN KEY (country_code) REFERENCES countries (code));
INSERT INTO countries VALUES ('KR', 'Korea'), ('JP', 'Japan');
CREATE TABLE import_customers (id INT, name VARCHAR(50), country_code VARCHAR(5));
INSERT INTO import_customers VALUES (1, 'Kim', 'KR'), (2, 'Sato', 'JP ');
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.01 sec)

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
INSERT INTO customers SELECT id, name, country_code FROM import_customers;
✕ ERROR
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`lab_a`.`customers`, CONSTRAINT `fk_cust_country` FOREIGN KEY (`country_code`) REFERENCES `countries` (`code`))
Check the causerun as lab_a
SELECT id, CONCAT('[', country_code, ']') AS code, LENGTH(country_code) AS len FROM import_customers ORDER BY id;
OUTPUT
+------+-------+------+
| id   | code  | len  |
+------+-------+------+
|    1 | [KR]  |    2 |
|    2 | [JP ] |    3 |
+------+-------+------+
2 rows in set (0.00 sec)

Wrapping the value in [ ] shows [JP ], and LENGTH is 3 instead of 2.

Fixrun as lab_a
UPDATE import_customers SET country_code = TRIM(country_code);
OUTPUT
Query OK, 1 row affected (0.00 sec)
Rows matched: 2  Changed: 1  Warnings: 0
After the fixrun as lab_a
INSERT INTO customers SELECT id, name, country_code FROM import_customers;
SELECT id, name, country_code FROM customers ORDER BY id;
✓ SUCCESS
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

+----+------+--------------+
| id | name | country_code |
+----+------+--------------+
|  1 | Kim  | KR           |
|  2 | Sato | JP           |
+----+------+--------------+
2 rows in set (0.00 sec)

FAQ

How do I fix MySQL error 1452 Cannot add or update a child row?

Read the FK column and parent table from the message, then check that the value you insert exists in the parent table. Insert the parent row first or correct the value.

Why does ERROR 1452 happen when I add a foreign key?

The table already contains rows whose value is missing from the parent table. Find them with a LEFT JOIN where the parent id IS NULL, fix or clear them, and run the ALTER again.

What is #sql-xxx in the 1452 error message?

It is the temporary copy MySQL creates while running ALTER TABLE. The real table is the one in your ALTER statement.

Is SET FOREIGN_KEY_CHECKS = 0 safe?

It only affects your session and is useful for load order, but rows loaded while it is off are not checked later. Load all parent rows and set it back to 1.

Why does 1452 happen when the value looks correct?

Check for hidden spaces: wrap the value like CONCAT('[', col, ']') and compare LENGTH. A trailing space makes it a different key. Also check for 0 used instead of NULL for "no parent".

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