Skip to content

[Bug]: TRUNCATE on a self-referencing foreign key table resolves table ID 0 #26838

Closed

Description

@Ariznawlll

Description

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

drop database if exists repro_truncate_self_fk;
create database repro_truncate_self_fk;
use repro_truncate_self_fk;

create table tree(
  id int primary key,
  parent_id int null,
  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

  1. added this to the MO-202608 milestone on Aug 9, 2026
  2. added
    kind/bug
    on Aug 9, 2026
  3. added theissue type on Aug 9, 2026
  4. added
    severity/s0
    and removed on Aug 9, 2026
  5. assigned and unassigned on Aug 11, 2026
  6. Ariznawlll commented on Aug 11, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:d01c85974f9540f8baa2e768ca86fea952941fe2
    • 部署方式:本地 mo-service,1 Log / 1 TN / 1 CN,MySQL 文本协议
    • 测试日期:2026-08-11

    原问题验证

    在自引用 FK 表中分别使用 CASCADE 和 SET NULL 动作执行 TRUNCATE;三轮均成功,表行数归零。

    Happy Path 覆盖

    1. 两种自引用动作均能清空 (1,NULL),(2,1),(3,2) 树形数据。

    Unhappy Path 覆盖

    1. 原先会解析内部 table ID 0 的路径不再返回 20101,操作后重新读取确认没有残留行。

    白盒验证

    不适用:通过公开 FK DDL 与 TRUNCATE 验证实际执行路径。

    回归测试

    已加入 suites/14_issue_regression/t/issue_26820_26825_26827_26838_26839_26896_sql_semantics.test,PR #89,提交 f6c496413efcbb7807d85643ac50cb836139a7c1。

    测试证据

    该 motr 用例与同批用例连续 3 轮通过,共 216 条断言成功。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions