Bug #121917 NULL non-FK index part bypasses foreign key checks and cascades

Submitted: 24 Nov 23:25
Reporter: QiFan Liu Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: Foreign Keys Severity:S2 (Serious)
Version:26.10.0 OS:Linux
Assigned to: CPU Architecture:x86
Tags: cascade, foreign key, MATCH SIMPLE, null, supporting index

[24 Nov 23:25] QiFan Liu
Description:
When a supporting index contains a nullable column after the foreign key columns, NULL in that extra non-FK column is incorrectly treated as satisfying MATCH SIMPLE.

For a child supporting index (parent_id, nullable_tail) and a foreign key containing only parent_id, an INSERT and an UPDATE with nonexistent non-NULL parent_id values succeed when nullable_tail is NULL. The query below reports validation_orphans=2; both writes should be rejected.

The same problem affects parent actions when a nonstandard referenced index is (id, nullable_tail), while the foreign key references only id. If nullable_tail is NULL, ON DELETE CASCADE, ON UPDATE CASCADE, and the delete portion of REPLACE do not process the child rows.

The observed output is:

validation_orphans
2
action_orphans
6
delete_not_cascaded  update_not_cascaded  replace_not_cascaded
2                    2                     2

The extra index column is not part of either foreign key, so its NULL value should not exempt the non-NULL foreign key columns from validation or suppress referential actions.

How to repeat:
Run as root in a fresh mysql session:

CREATE DATABASE db1;
USE db1;

CREATE TABLE validation_parent (
  id INT NOT NULL PRIMARY KEY
) ENGINE=InnoDB;

CREATE TABLE validation_child (
  child_id INT NOT NULL PRIMARY KEY,
  parent_id INT NOT NULL,
  nullable_tail INT NULL,
  KEY validation_support (parent_id, nullable_tail),
  CONSTRAINT validation_fk
    FOREIGN KEY (parent_id)
    REFERENCES validation_parent (id)
) ENGINE=InnoDB;

INSERT INTO validation_parent VALUES (1);

-- Succeeds, but should be rejected because parent_id=999 has no parent.
INSERT INTO validation_child VALUES (10, 999, NULL);

INSERT INTO validation_child VALUES (11, 1, NULL);

-- Succeeds, but should be rejected because parent_id=998 has no parent.
UPDATE validation_child
SET parent_id = 998
WHERE child_id = 11;

SELECT COUNT(*) AS validation_orphans
FROM validation_child AS c
LEFT JOIN validation_parent AS p ON p.id = c.parent_id
WHERE p.id IS NULL;
-- Returns 2; expected 0.

SET SESSION restrict_fk_on_non_standard_key = OFF;

CREATE TABLE action_parent (
  row_id INT NOT NULL PRIMARY KEY,
  id INT NOT NULL,
  nullable_tail INT NULL,
  KEY action_parent_support (id, nullable_tail)
) ENGINE=InnoDB;

CREATE TABLE action_child (
  child_id INT NOT NULL PRIMARY KEY,
  parent_id INT NOT NULL,
  KEY action_child_support (parent_id),
  CONSTRAINT action_fk
    FOREIGN KEY (parent_id)
    REFERENCES action_parent (id)
    ON DELETE CASCADE
    ON UPDATE CASCADE
) ENGINE=InnoDB;

INSERT INTO action_parent VALUES
  (1, 7, NULL),
  (2, 8, NULL),
  (3, 9, NULL);

INSERT INTO action_child VALUES
  (101, 7), (102, 7),
  (201, 8), (202, 8),
  (301, 9), (302, 9);

DELETE FROM action_parent WHERE row_id = 1;
UPDATE action_parent SET id = 80 WHERE row_id = 2;
REPLACE INTO action_parent VALUES (3, 90, NULL);

SELECT COUNT(*) AS action_orphans
FROM action_child AS c
LEFT JOIN action_parent AS p ON p.id = c.parent_id
WHERE p.id IS NULL;
-- Returns 6; expected 0.

SELECT
  SUM(parent_id = 7) AS delete_not_cascaded,
  SUM(parent_id = 8) AS update_not_cascaded,
  SUM(parent_id = 9) AS replace_not_cascaded
FROM action_child;
-- Returns 2, 2, 2; expected 0, 0, 0.

Suggested fix:
Apply MATCH SIMPLE null detection only to the logical foreign key columns, not to additional parts of the supporting index.

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

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