Bug #121794 Foreign key not enforced in 9.7 when the child FK columns prefix an index that contains a nullable column

Submitted: 17 Nov 0:04
Reporter: Adam Hughey Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: DDL Severity:S2 (Serious)
Version:9.7.4 OS:Any (Linux: Docker mysql:9.7 (arm64) and Google Cloud SQL 9.7.2-google)
Assigned to: CPU Architecture:Any (arm64 (Docker on Apple Silicon) and Google Cloud SQL (Google-managed hosts))
Tags: foreign key, innodb_native_foreign_keys, referential integrity, regression

[17 Nov 0:04] Adam Hughey
Description:
On MySQL 9.7, a foreign key is not enforced when the child table's foreign-key columns are the leading columns of a secondary index that also contains a nullable non-key column, and the inserted row has NULL in that column. The INSERT succeeds even though no parent row exists. MySQL 8.4.13 rejects the same statement with ERROR 1452 (23000), "Cannot add or update a child row: a foreign key constraint fails".

It affects both composite and single-column foreign keys. A control table whose wider index holds only NOT NULL columns (tenant_id, created_by, id) is enforced correctly on 9.7.

Same results on:
- the official Docker image mysql:9.7 (VERSION() = 9.7.4), with default settings;
- Google Cloud SQL for MySQL 9.7 (VERSION() = 9.7.2-google), innodb_native_foreign_keys = 0.
MySQL 8.4.13 (Docker mysql:8.4) rejects all three cases.

This may relate to the 9.6 change that moved foreign-key checks from InnoDB to the SQL layer (innodb_native_foreign_keys defaults to OFF). A related report of a skipped SQL-layer FK check (REPLACE on a parent table, 9.7.1): https://github.com/mysql/mysql-server/issues/869. A third-party project reported this same symptom on 9.7.2: https://github.com/kuasar-mknd/gym-tracker/issues/2095

The impact is silent loss of referential integrity: rows can reference parents that do not exist, for example a row in one tenant pointing at a user in another tenant when the FK is (tenant_id, user_id).

How to repeat:
CREATE DATABASE fkbug;
USE fkbug;

CREATE TABLE parent (
  id INT PRIMARY KEY,
  tenant_id INT NULL,
  UNIQUE KEY tu (tenant_id, id)
) ENGINE=InnoDB;
INSERT INTO parent VALUES (5, 2), (6, 3);

-- Case 1: composite FK; the FK columns prefix an index that ends with a nullable column
CREATE TABLE child1 (
  id INT AUTO_INCREMENT PRIMARY KEY,
  tenant_id INT NOT NULL,
  created_by INT NOT NULL,
  extra INT NULL,
  KEY ix (tenant_id, created_by, extra, id),
  CONSTRAINT fk1 FOREIGN KEY (tenant_id, created_by) REFERENCES parent (tenant_id, id)
) ENGINE=InnoDB;
INSERT INTO child1 (tenant_id, created_by) VALUES (2, 6);
-- 8.4.13: ERROR 1452. 9.7.4 / 9.7.2: Query OK (parent (2, 6) does not exist).

-- Case 2: same, index without the trailing id
CREATE TABLE child2 (
  id INT AUTO_INCREMENT PRIMARY KEY,
  tenant_id INT NOT NULL,
  created_by INT NOT NULL,
  extra INT NULL,
  KEY ix (tenant_id, created_by, extra),
  CONSTRAINT fk2 FOREIGN KEY (tenant_id, created_by) REFERENCES parent (tenant_id, id)
) ENGINE=InnoDB;
INSERT INTO child2 (tenant_id, created_by) VALUES (2, 6);
-- 8.4.13: ERROR 1452. 9.7.4 / 9.7.2: Query OK.

-- Case 3: single-column FK; the FK column prefixes an index with a nullable column
CREATE TABLE child3 (
  id INT AUTO_INCREMENT PRIMARY KEY,
  tenant_id INT NOT NULL,
  created_by INT NOT NULL,
  extra INT NULL,
  KEY ix (created_by, extra),
  CONSTRAINT fk3 FOREIGN KEY (created_by) REFERENCES parent (id)
) ENGINE=InnoDB;
INSERT INTO child3 (tenant_id, created_by) VALUES (2, 99);
-- 8.4.13: ERROR 1452. 9.7.4 / 9.7.2: Query OK (parent id 99 does not exist).

-- Control: the wider index holds only NOT NULL columns; enforced correctly on 9.7
CREATE TABLE child4 (
  id INT AUTO_INCREMENT PRIMARY KEY,
  tenant_id INT NOT NULL,
  created_by INT NOT NULL,
  extra INT NULL,
  KEY ix (tenant_id, created_by, id),
  CONSTRAINT fk4 FOREIGN KEY (tenant_id, created_by) REFERENCES parent (tenant_id, id)
) ENGINE=InnoDB;
INSERT INTO child4 (tenant_id, created_by) VALUES (2, 6);
-- 8.4.13 and 9.7: ERROR 1452 (correct).

-- Expected: every child INSERT above fails with ERROR 1452.
-- Actual on 9.7.4 and 9.7.2: child1, child2 and child3 succeed; only child4 fails.

Suggested fix:
Possible cause, unverified (inferred only from which shapes fail): the SQL-layer check may treat a NULL anywhere in the chosen child-side index as a NULL foreign-key value and skip the parent lookup. Only a NULL in one of the foreign key's own columns should exempt a row.

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.040 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.