You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: ON DELETE SET NULL on a NOT NULL FK column silently leaves an orphan child #30282
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=2WHERE 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.
After adding the delete action, information_schema.KEY_COLUMN_USAGE records fk_parent, and:
DELETEFROM 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=2WHERE 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
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:
CREATETABLEparent(id INTPRIMARY KEY);
CREATETABLEchild(
id INTPRIMARY KEY,
parent_id INT DEFAULT NULL,
KEY idx_parent(parent_id),
CONSTRAINT fk_parent FOREIGN KEY(parent_id)
REFERENCES parent(id) ON DELETESETNULL
);
INSERT INTO parent VALUES (1);
INSERT INTO child VALUES (10,1);
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:
DELETEFROM 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.
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.
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.
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.
Activity
Ariznawlll commentedon Oct 15, 2026
Additional evidence on the same missing DDL validation, using the
ON UPDATE SET NULLside of the same action:MatrixOne actual
At commit
501c85ecdd6eee0195172743c6d8aaf125cb252d, the invalid child definition is accepted.After inserting
parent (1)andchild (10,1), both an explicit transaction and an autocommit statement:fail with error 3819:
This reproduced 3/3. In the explicit-transaction run, the parent remains
id=1, the child remains(10,1), a followingINSERT INTO child VALUES(99,1)succeeds, and afterROLLBACKthe original rows are unchanged. The failed action does not leave an orphan or dirty the open transaction.With a nullable
parent_id INTcontrol, the same update succeeds 3/3, changes the parent toid=2, and sets the child toparent_id=NULL, as expected.MySQL 8.0.44 comparison
MySQL rejects the DDL immediately, 3/3:
This confirms the missing validation covers both
ON DELETE SET NULLandON UPDATE SET NULLdeclarations. 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.Ariznawlll commentedon Oct 15, 2026
Additional evidence that the same invalid definition can also enter through
ALTER TABLE ADD CONSTRAINT.Starting with this schema:
MatrixOne accepts either declaration at commit
501c85ecdd6eee0195172743c6d8aaf125cb252d:ON DELETE
After adding the delete action,
information_schema.KEY_COLUMN_USAGErecordsfk_parent, and: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 TABLEshows:Then:
fails 3/3 with:
After the explicit-transaction run is rolled back, the parent remains
id=1and the child remains(10,1). The autocommit attempt also leaves no orphan.Controls and MySQL
The same
ALTER TABLEdefinitions succeed 3/3 whenparent_idis nullable: the update action sets its child toNULL, and the delete action sets its child toNULL.MySQL 8.0.44 rejects both
ALTER TABLEdefinitions 3/3 with:Ariznawlll commentedon Oct 15, 2026
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 NULLconstraint.Start with a valid nullable constraint:
Then execute:
At commit
501c85ecdd6eee0195172743c6d8aaf125cb252d, MatrixOne accepts this statement.information_schema.COLUMNSreportsparent_idasNOT NULLwhileKEY_COLUMN_USAGEstill reportsfk_parent. After: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 NULLinstead produces the same runtime split as before: after the modification,UPDATE parent SET id=2 WHERE id=1fails 3/3 with: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 NULLmodification, the update action changes the child toNULL, and the delete action changes the child toNULL.MySQL 8.0.44 rejects the
MODIFY COLUMN ... NOT NULLstatement immediately, 3/3:The same
SET NULLdefinitions are accepted by both systems whileparent_idremains nullable.test(bvt): record FK 1830 rejection as native MySQL error text (matri…
fix(plan): reject FK ON DELETE/UPDATE SET NULL over NOT NULL child co…
test(bvt): record FK 1830 rejection as native MySQL error text (matri…
fix(plan): reject ALTER tightening under surviving SET NULL FK (matri…
fix(plan): compare pre-ALTER nullability by column identity, not name (…
fix(plan): keep TRUNCATE/unfiltered-DELETE legacy FK layouts rebuilda…
fix(plan): reject FK ON DELETE/UPDATE SET NULL over NOT NULL child co…
XuPeng-SH commentedon Oct 24, 2026
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-serviceSHA-25662f804982b37db9a24f42d68b733d8b35075c097fb635510e6ec728ac495fb09.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 NULLcreate/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 nullableON DELETE SET NULLandON UPDATE SET NULLboth 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.
daviszhen commentedon Oct 24, 2026
Fixed by #30925 (merged as
73ab0a9158).Root cause. Two layers:
checkAlterSurvivingFkSetNullkeyed the old-nullability exemption by column name, soDROP 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.SHOW CREATEthrough 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
TrustedForeignKeyLayoutbridge (cloned from theTrustedCheckConstraintNamespattern) 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).
TestIssue30282AlterNameReuseCannotBypassSetNullGuard,TestIssue30282TrustedForeignKeyLayoutRebuild).foreign_key/fk_issue_30282.{sql,result}; goldens replayed byte-identical on a live server.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.4af80f2dc5; mo-bot approved.daviszhen commentedon Oct 24, 2026
Closing as completed via #30925 (merged '73ab0a9158'); full fix + evidence in the comment above.