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]: TRUNCATE on a self-referencing foreign key table resolves table ID 0 #26838
TRUNCATE TABLE fails with an internal table-ID lookup error for every self-referencing foreign key action. The planner correctly recognizes that a table referenced only by itself may be truncated, but then passes the self-reference sentinel table ID 0 into execution as a real parent table ID.
Reproduced three consecutive times again on current main commit 45a917d7f8f7b2f32c28ca0a734318875722a00e.
Reproduction
dropdatabase if exists repro_truncate_self_fk;
createdatabaserepro_truncate_self_fk;
use repro_truncate_self_fk;
createtabletree(
id intprimary key,
parent_id intnull,
foreign key(parent_id) references tree(id) on delete cascade
);
insert into tree values (1,null),(2,1),(3,2);
truncate table tree;
select*from tree order by id;
Actual result
ERROR 20101 (HY000): internal error: can not find table by id 0: accountId: 0
All three rows remain. The same result occurs with self-referencing ON DELETE SET NULL and ON DELETE RESTRICT. Repeating TRUNCATE three times is deterministic. DROP TABLE for an equivalent self-referencing table is a passing control.
Expected result
TRUNCATE should succeed for a table whose only foreign-key child is itself and should remove all rows.
Analysis
The TRUNCATE planner permits the operation when HasFkSelfReferOnly(tableDef) is true. It then appends every fk.ForeignTbl to truncateTable.ForeignTbl; a self-reference is encoded as table ID 0. The DROP TABLE path explicitly skips ForeignTbl == 0, but the TRUNCATE path does not, so execution attempts to resolve table ID 0.
Activity
fix: preserve table integrity across ALTER and TRUNCATE (#26893)
Ariznawlll commentedon Aug 11, 2026
测试版本
maind01c85974f9540f8baa2e768ca86fea952941fe2mo-service,1 Log / 1 TN / 1 CN,MySQL 文本协议原问题验证
在自引用 FK 表中分别使用 CASCADE 和 SET NULL 动作执行 TRUNCATE;三轮均成功,表行数归零。
Happy Path 覆盖
(1,NULL),(2,1),(3,2)树形数据。Unhappy Path 覆盖
白盒验证
不适用:通过公开 FK DDL 与 TRUNCATE 验证实际执行路径。
回归测试
已加入
suites/14_issue_regression/t/issue_26820_26825_26827_26838_26839_26896_sql_semantics.test,PR #89,提交f6c496413efcbb7807d85643ac50cb836139a7c1。测试证据
该 motr 用例与同批用例连续 3 轮通过,共 216 条断言成功。
测试结论
测试通过。
fix: preserve table integrity across ALTER and TRUNCATE (matrixorigin…
fix: preserve table integrity across ALTER and TRUNCATE (matrixorigin…
fix: preserve table integrity across ALTER and TRUNCATE (matrixorigin…