Skip to content

[Bug]: ON DELETE SET NULL on a NOT NULL FK column silently leaves an orphan child #30282

Closed

Description

@Ariznawlll

@round_041_set_null_issue.md

Activity

  1. added this to the MO-202610 milestone on Oct 15, 2026
  2. Ariznawlll commented on Oct 15, 2026

    @Ariznawlll
    CollaboratorAuthor

    Additional evidence on the same missing DDL validation, using the ON UPDATE SET NULL side of the same action:

    CREATE DATABASE round_042_update_set_null;
    USE round_042_update_set_null;
    
    CREATE TABLE parent(id INT PRIMARY KEY);
    CREATE TABLE child(
        id INT PRIMARY KEY,
        parent_id INT NOT NULL,
        KEY idx_parent(parent_id),
        CONSTRAINT fk_parent FOREIGN KEY(parent_id)
            REFERENCES parent(id) ON UPDATE SET NULL
    );

    MatrixOne actual

    At commit 501c85ecdd6eee0195172743c6d8aaf125cb252d, the invalid child definition is accepted.

    After inserting parent (1) and child (10,1), both an explicit transaction and an autocommit statement:

    UPDATE parent SET id=2 WHERE id=1;

    fail with error 3819:

    ERROR 3819 (HY000): constraint violation: Column 'parent_id' cannot be null
    

    This reproduced 3/3. In the explicit-transaction run, the parent remains id=1, the child remains (10,1), a following INSERT INTO child VALUES(99,1) succeeds, and after ROLLBACK the original rows are unchanged. The failed action does not leave an orphan or dirty the open transaction.

    With a nullable parent_id INT control, the same update succeeds 3/3, changes the parent to id=2, and sets the child to parent_id=NULL, as expected.

    MySQL 8.0.44 comparison

    MySQL rejects the DDL immediately, 3/3:

    ERROR 1830 (HY000): Column 'parent_id' cannot be NOT NULL: needed in a foreign key constraint 'fk_parent' SET NULL
    

    This confirms the missing validation covers both ON DELETE SET NULL and ON UPDATE SET NULL declarations. The user-visible consequence differs by path: the invalid delete action silently preserves the old child value after deleting the parent, while the invalid update action reaches runtime and fails with 3819.

  3. Ariznawlll commented on Oct 15, 2026

    @Ariznawlll
    CollaboratorAuthor

    Additional evidence that the same invalid definition can also enter through ALTER TABLE ADD CONSTRAINT.

    Starting with this schema:

    CREATE TABLE parent(id INT PRIMARY KEY);
    CREATE TABLE child(
        id INT PRIMARY KEY,
        parent_id INT NOT NULL,
        KEY idx_parent(parent_id)
    );
    INSERT INTO parent VALUES (1);
    INSERT INTO child VALUES (10,1);

    MatrixOne accepts either declaration at commit 501c85ecdd6eee0195172743c6d8aaf125cb252d:

    ALTER TABLE child
        ADD CONSTRAINT fk_parent FOREIGN KEY(parent_id)
        REFERENCES parent(id) ON DELETE SET NULL;
    
    ALTER TABLE child
        ADD CONSTRAINT fk_parent FOREIGN KEY(parent_id)
        REFERENCES parent(id) ON UPDATE SET NULL;

    ON DELETE

    After adding the delete action, information_schema.KEY_COLUMN_USAGE records fk_parent, and:

    DELETE FROM parent WHERE id=1;

    succeeds. The child remains (10,1) while the parent is gone, leaving one orphan row. This reproduced 3/3. An explicit transaction that deletes the parent also leaves the child unchanged before rollback, so the bad action is not limited to autocommit execution.

    ON UPDATE

    After adding the update action, SHOW CREATE TABLE shows:

    CONSTRAINT `fk_parent` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`) ON DELETE RESTRICT ON UPDATE SET NULL
    

    Then:

    UPDATE parent SET id=2 WHERE id=1;

    fails 3/3 with:

    ERROR 3819 (HY000): constraint violation: Column 'parent_id' cannot be null
    

    After the explicit-transaction run is rolled back, the parent remains id=1 and the child remains (10,1). The autocommit attempt also leaves no orphan.

    Controls and MySQL

    The same ALTER TABLE definitions succeed 3/3 when parent_id is nullable: the update action sets its child to NULL, and the delete action sets its child to NULL.

    MySQL 8.0.44 rejects both ALTER TABLE definitions 3/3 with:

    ERROR 1830 (HY000): Column 'parent_id' cannot be NOT NULL: needed in a foreign key constraint 'fk_parent' SET NULL
    
  4. Ariznawlll commented on Oct 15, 2026

    @Ariznawlll
    CollaboratorAuthor

    The same invalid state can also be reached from the opposite direction by modifying a child FK column that already belongs to a valid SET NULL constraint.

    Start with a valid nullable constraint:

    CREATE TABLE parent(id INT PRIMARY KEY);
    CREATE TABLE child(
        id INT PRIMARY KEY,
        parent_id INT DEFAULT NULL,
        KEY idx_parent(parent_id),
        CONSTRAINT fk_parent FOREIGN KEY(parent_id)
            REFERENCES parent(id) ON DELETE SET NULL
    );
    INSERT INTO parent VALUES (1);
    INSERT INTO child VALUES (10,1);

    Then execute:

    ALTER TABLE child MODIFY COLUMN parent_id INT NOT NULL;

    At commit 501c85ecdd6eee0195172743c6d8aaf125cb252d, MatrixOne accepts this statement. information_schema.COLUMNS reports parent_id as NOT NULL while KEY_COLUMN_USAGE still reports fk_parent. After:

    DELETE FROM parent WHERE id=1;

    the parent is deleted and the child remains (10,1), leaving one orphan. This reproduced 3/3. In the explicit-transaction run, the child remains (10,1) before rollback; rollback restores both original rows.

    Using ON UPDATE SET NULL instead produces the same runtime split as before: after the modification, UPDATE parent SET id=2 WHERE id=1 fails 3/3 with:

    ERROR 3819 (HY000): constraint violation: Column 'parent_id' cannot be null
    

    The open transaction rolls back cleanly, and the autocommit attempt leaves the original rows unchanged.

    The nullable control behaves correctly 3/3: without the NOT NULL modification, the update action changes the child to NULL, and the delete action changes the child to NULL.

    MySQL 8.0.44 rejects the MODIFY COLUMN ... NOT NULL statement immediately, 3/3:

    ERROR 1830 (HY000): Column 'parent_id' cannot be NOT NULL: needed in a foreign key constraint 'fk_parent' SET NULL
    

    The same SET NULL definitions are accepted by both systems while parent_id remains nullable.

  5. XuPeng-SH commented on Oct 24, 2026

    @XuPeng-SH
    Contributor

    Current-main local validation for the fix merged in PR #30925.

    Runtime: fresh isolated 1 Log / 1 TN / 1 CN service, built from main 73ab0a9158dcbfb6b1ed29dd17ee4140342e738c; mo-service SHA-256 62f804982b37db9a24f42d68b733d8b35075c097fb635510e6ec728ac495fb09.

    SQL evidence: replayed the committed test/distributed/cases/foreign_key/fk_issue_30282.sql (67 statements) on MySQL port 62720. All 13 invalid NOT NULL + SET NULL create/alter cases returned the expected exact error 1830; all 9 metadata assertions matched; there were no unexpected errors or result mismatches. Two additional DML controls confirmed nullable ON DELETE SET NULL and ON UPDATE SET NULL both set the child FK to NULL and apply the parent change.

    The reported invalid DDL is rejected in this build. Scope: one fresh local CN and direct SQL; this does not include a cross-version MySQL comparison.

  6. daviszhen commented on Oct 24, 2026

    @daviszhen
    Contributor

    Fixed by #30925 (merged as 73ab0a9158).

    Root cause. Two layers:

    1. checkAlterSurvivingFkSetNull keyed the old-nullability exemption by column name, so DROP COLUMN other, CHANGE pid other ... NOT NULL — and rename-away variants — inherited the exemption of the dropped same-name column and bypassed the NOT NULL + SET NULL guard with live child rows.
    2. After gating, internal reconstruction paths (TRUNCATE, unfiltered-DELETE recreate) replayed the table's SHOW CREATE through the same guard without any trust marker, so a legal legacy NOT NULL + SET NULL table could no longer be truncated.

    Fix. Old nullability is re-keyed by ColId identity (CHANGE/RENAME preserve ColId; name reuse of a dropped column no longer inherits the exemption), and a statement-scoped TrustedForeignKeyLayout bridge (cloned from the TrustedCheckConstraintNames pattern) lets internal executor-driven recreates skip the guard while user-facing ALTER/CREATE still reject the layout.

    Evidence that went with the PR (each at its exact head).

    • Plan-layer unit tests for the name-reuse bypass and its legalized control (TestIssue30282AlterNameReuseCannotBypassSetNullGuard, TestIssue30282TrustedForeignKeyLayoutRebuild).
    • BVT foreign_key/fk_issue_30282.{sql,result}; goldens replayed byte-identical on a live server.
    • Live verification with a pristine old binary seeding the legacy layout, then the new binary: both bypass variants rejected with ERROR 1830, DROP FOREIGN KEY + ALTER legalized control works, TRUNCATE and unfiltered DELETE recreate succeed with the FK intact, and user CREATE of the layout still errors.
    • CI green at 4af80f2dc5; mo-bot approved.
  7. daviszhen commented on Oct 24, 2026

    @daviszhen
    Contributor

    Closing as completed via #30925 (merged '73ab0a9158'); full fix + evidence in the comment above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/compatibilitykind/bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions