Bug #121859 ON DELETE CASCADE leaves utf8mb4 VARCHAR children of CHAR parent
Description:
An InnoDB ON DELETE CASCADE constraint fails when a CHAR(4) parent key is referenced by a VARCHAR(4) child key under the utf8mb4_0900_ai_ci collation and the value consists of four four-byte characters.
Both child rows are accepted by the foreign key. Deleting the parent succeeds, but neither child is deleted, leaving two orphan rows:
mysql> SELECT COUNT(*) AS remaining_children FROM child;
+--------------------+
| remaining_children |
+--------------------+
| 2 |
+--------------------+
mysql> SELECT COUNT(*) AS orphan_children
-> FROM child AS c
-> LEFT JOIN parent AS p ON p.parent_key = c.parent_key
-> WHERE p.parent_key IS NULL;
+-----------------+
| orphan_children |
+-----------------+
| 2 |
+-----------------+
Both counts should be zero after the cascade.
How to repeat:
Run as root:
DROP DATABASE IF EXISTS db1;
CREATE DATABASE db1
CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
USE db1;
CREATE TABLE parent (
parent_key CHAR(4) NOT NULL PRIMARY KEY
) ENGINE=InnoDB;
CREATE TABLE child (
child_id INT NOT NULL PRIMARY KEY,
parent_key VARCHAR(4) NOT NULL,
KEY child_parent_key (parent_key),
CONSTRAINT fk_child_parent
FOREIGN KEY (parent_key)
REFERENCES parent (parent_key)
ON DELETE CASCADE
) ENGINE=InnoDB;
SET @emoji4 =
CONVERT(0xF09F9880F09F9880F09F9880F09F9880 USING utf8mb4);
INSERT INTO parent VALUES (@emoji4);
INSERT INTO child VALUES
(1, @emoji4),
(2, @emoji4);
DELETE FROM parent WHERE parent_key = @emoji4;
SELECT COUNT(*) AS remaining_children FROM child;
-- returns 2, expected 0
SELECT COUNT(*) AS orphan_children
FROM child AS c
LEFT JOIN parent AS p ON p.parent_key = c.parent_key
WHERE p.parent_key IS NULL;
-- returns 2, expected 0
Suggested fix:
When constructing the parent-side CHAR-to-VARCHAR search key under a NO PAD collation, calculate and copy the complete value length in bytes so the exact child lookup receives the full multibyte key.
Portions of this website are copyright © 2001, 2002 The PHP Group
Page generated in 0.046 sec. using MySQL 8.4.7-u6-cloud
Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the
respective copyright holders.
It is not reviewed in advance
by Oracle and does not necessarily represent the opinion of
Oracle or any other party.
Description: An InnoDB ON DELETE CASCADE constraint fails when a CHAR(4) parent key is referenced by a VARCHAR(4) child key under the utf8mb4_0900_ai_ci collation and the value consists of four four-byte characters. Both child rows are accepted by the foreign key. Deleting the parent succeeds, but neither child is deleted, leaving two orphan rows: mysql> SELECT COUNT(*) AS remaining_children FROM child; +--------------------+ | remaining_children | +--------------------+ | 2 | +--------------------+ mysql> SELECT COUNT(*) AS orphan_children -> FROM child AS c -> LEFT JOIN parent AS p ON p.parent_key = c.parent_key -> WHERE p.parent_key IS NULL; +-----------------+ | orphan_children | +-----------------+ | 2 | +-----------------+ Both counts should be zero after the cascade. How to repeat: Run as root: DROP DATABASE IF EXISTS db1; CREATE DATABASE db1 CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci; USE db1; CREATE TABLE parent ( parent_key CHAR(4) NOT NULL PRIMARY KEY ) ENGINE=InnoDB; CREATE TABLE child ( child_id INT NOT NULL PRIMARY KEY, parent_key VARCHAR(4) NOT NULL, KEY child_parent_key (parent_key), CONSTRAINT fk_child_parent FOREIGN KEY (parent_key) REFERENCES parent (parent_key) ON DELETE CASCADE ) ENGINE=InnoDB; SET @emoji4 = CONVERT(0xF09F9880F09F9880F09F9880F09F9880 USING utf8mb4); INSERT INTO parent VALUES (@emoji4); INSERT INTO child VALUES (1, @emoji4), (2, @emoji4); DELETE FROM parent WHERE parent_key = @emoji4; SELECT COUNT(*) AS remaining_children FROM child; -- returns 2, expected 0 SELECT COUNT(*) AS orphan_children FROM child AS c LEFT JOIN parent AS p ON p.parent_key = c.parent_key WHERE p.parent_key IS NULL; -- returns 2, expected 0 Suggested fix: When constructing the parent-side CHAR-to-VARCHAR search key under a NO PAD collation, calculate and copy the complete value length in bytes so the exact child lookup receives the full multibyte key.