pingcap / pingcap/tidb

[Compatibility] `ON DUPLICATE KEY UPDATE` clause causes `INSERT IGNORE` to fail on Foreign Key violation

Open
#66,258 2 comments 0 reactions 1 assignee Claimed by @cryo-zd View on GitHub
contribution severity/moderate sig/sql-infra type/bug
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

Please answer these questions before submitting your issue. Thanks!

### 1. Minimal reproduce step (Required)
```SQL
mysql> CREATE TABLE t1 (s1 TINYINT PRIMARY KEY);
Query OK, 0 rows affected (0.02 sec)

mysql> CREATE TABLE t2 (s1 TINYINT UNIQUE KEY, FOREIGN KEY (s1) REFERENCES t1 (s1));
Query OK, 0 rows affected (0.02 sec)

mysql> INSERT INTO t1 VALUES (0);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO t2 VALUES (0);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT IGNORE INTO t2 VALUES (3);
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+---------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 1452 | Cannot add or update a child row: a foreign key constraint fails (`test`.`t2`, CONSTRAINT `fk_1` FOREIGN KEY (`s1`) REFERENCES `t1` (`s1`)) |
+---------+------+---------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

-- '3' does not exist in t2, so ODKU should not trigger.
mysql> INSERT IGNORE INTO t2 VALUES (3) ON DUPLICATE KEY UPDATE s1 = VALUES(s1);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test`.`t2`, CONSTRAINT `fk_1` FOREIGN KEY (`s1`) REFERENCES `t1` (`s1`))
```

### 2. What did you expect to see? (Required)
Both queries had better succeed with warnings.
```SQL
mysql> INSERT IGNORE INTO t2 VALUES (3) ON DUPLICATE KEY UPDATE s1 = VALUES(s1);
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> show warnings;
+---------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 1287 | 'VALUES function' is deprecated and will be removed in a future release. Please use an alias (INSERT INTO ... VALUES (...) AS alias) and replace VALUES(col) in the ON DUPLICATE KEY UPDATE clause with alias.col instead |
| Warning | 1452 | Cannot add or update a child row: a foreign key constraint fails (`test`.`t2`, CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`s1`) REFERENCES `t1` (`s1`)) |
+---------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.45 |
+-----------+
1 row in set (0.04 sec)
```

### 3. What did you see instead (Required)
```SQL
mysql> INSERT IGNORE INTO t2 VALUES (3) ON DUPLICATE KEY UPDATE s1 = VALUES(s1);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test`.`t2`, CONSTRAINT `fk_1` FOREIGN KEY (`s1`) REFERENCES `t1` (`s1`))
```

### 4. What is your TiDB version? (Required)
```SQL
mysql> select tidb_version();
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tidb_version() |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: v9.0.0-beta.2.pre-1236-gcbb1898ccf
Edition: Community
Git Commit Hash: cbb1898ccfebaac674438927157df8cc8e9247fa
Git Branch: master
UTC Build Time: 2026-02-13 02:04:43
GoVersion: go1.25.6
Race Enabled: false
Check Table Before Drop: false
Store: unistore
Kernel Type: Classic |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.