Skip to content

[Compatibility]: FK blocks replacing its bound parent key despite an equivalent UNIQUE #30342

Closed

Description

@Ariznawlll

Description

MatrixOne cannot drop the particular parent UNIQUE index recorded by a foreign key even when a second, fully equivalent UNIQUE index on the same columns remains. MySQL 8.0.45 allows the DROP and continues enforcing the FK through the remaining index. This is a DDL compatibility gap, not observed data corruption.

Environment

  • Branch: official main
  • Commit: 48dbe277b6d20216c1c513d03c5cbd681380a8a2 (git_version() = 48dbe277b6)
  • Deployment: isolated local 1 Log/1 TN/2 CN
  • Comparator: isolated MySQL 8.0.45

Steps to reproduce

CREATE TABLE p (id INT PRIMARY KEY, u INT,
                UNIQUE KEY uk1(u), UNIQUE KEY uk2(u));
CREATE TABLE c (id INT PRIMARY KEY, u INT,
                FOREIGN KEY (u) REFERENCES p(u));
INSERT INTO p VALUES (1,10);
INSERT INTO c VALUES (1,10);
ALTER TABLE p DROP INDEX uk1;

Actual behavior

MO returns ERROR 1553 (HY000): Cannot drop index 'uk1': needed in a foreign key constraint. uk2(u) remains available. mo_catalog.mo_foreign_keys.referenced_index_name identifies uk1 as the selected FK index.

An atomic replacement has the same gap: with only uk_old(u) initially, ALTER TABLE p DROP INDEX uk_old, ADD UNIQUE INDEX uk_new(u) is rejected by MO with 1553, whereas MySQL succeeds and preserves FK enforcement. A related PRIMARY-key variant (PRIMARY KEY(id) plus UNIQUE KEY uk_id(id)) rejects ALTER TABLE p DROP PRIMARY KEY in MO but succeeds in MySQL, which retains uk_id(id) and FK enforcement. MO leaves each original definition intact; neither path caused partial DDL.

Expected behavior

MySQL 8.0.45 executes the same DROP successfully, retains UNIQUE KEY uk2(u), and continues rejecting invalid child references. MO could safely permit this operation by atomically rebinding the FK to the remaining equivalent index, or document the bound-index restriction as an explicit compatibility limit.

Stability and controls

  • Reproducer: 3/3 independent parent/child UNIQUE fixtures. ALTER TABLE ... DROP INDEX and standalone DROP INDEX ... ON both reject the selected index in MO and succeed in MySQL. Reversing physical index-creation order did not remove the gap; the catalog's selected name, not creation order, governs MO's decision. A separate atomic DROP+ADD replacement and PRIMARY-to-UNIQUE transition reproduce the same incompatibility.
  • Normal control: when no alternative key exists, both engines reject with 1553. In MO, dropping the unbound equivalent UNIQUE succeeds and FK enforcement stays active.
  • Failed MO DDL leaves the selected index and FK intact; no partial mutation was observed.

Evidence

Exact SQL, three variants, controls, and catalog/code evidence.

Code analysis

pkg/sql/plan/build_ddl.go:CheckDropReferencedKeyForeignKeyDependency protects the exact ForeignKeyDef.ReferencedIndexName. The current planner UT also asserts that a bound index is not droppable even if another compatible UNIQUE exists. This is a safety-preserving current implementation choice, but it does not provide MySQL's alternative-index transition.

Regression coverage

If compatibility is added, cover a bound-index DROP with an equivalent surviving UNIQUE, a same-statement DROP+ADD replacement, PRIMARY-to-UNIQUE transition, reversed index creation order, standalone/ALTER syntax, post-DDL child FK checks, and the no-alternative rejection control. No repository test was added during this exploration round.

Related

Activity

  1. added this to the MO-202610 milestone on Oct 16, 2026
  2. added
    kind/bug
    on Oct 16, 2026
  3. added theissue type on Oct 16, 2026
  4. changed the title [-][Compatibility]: DROP INDEX cannot switch an FK to an equivalent UNIQUE index[/-] [+][Compatibility]: FK blocks replacing its bound parent key despite an equivalent UNIQUE[/+] on Oct 16, 2026
  5. 2 remaining items

  6. added a commit that references this issue on Oct 29, 2026
    969998e
  7. XuPeng-SH commented on Oct 29, 2026

    @XuPeng-SH
    Contributor

    QA handoff for merged #31628 (969998e69776e7809ed1c56f8bfc62a9a98aac73): this isolated QA run inspected the source and issue history but did not run current-head functional tests or live SQL because the existing resource guard left insufficient headroom. Please verify FK surviving-key rebinding, self-reference, atomicity and multi-CN visibility. Author-reported PR tests are separate evidence and are not results of this QA run.

  8. Ariznawlll commented on Nov 3, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    测试通过。删除被外键引用的父表键时,可重新绑定到等价唯一键并继续执行外键约束。

    关键证据

    • 基于官方 main@d61a7206c16b30baa4efc525eb6067c4364035aa,隔离 MO 两 CN。
    • **Happy case:**被绑定唯一键删除后 catalog 改绑到剩余唯一键;同一 ALTER 中 DROP+ADD 后绑定新键并可正常插入;另一 CN 可观察新绑定。新增 MOTR #305 场景在每个 CN 各三轮通过,跨 CN 变更/读回三轮通过。
    • **Unhappy case:**改绑后孤儿子行被拒绝;删除最后一个可用键返回 1553;既有行保持完整。主库普通重绑、自引用与复合 ALTER 三套 BVT 值比较每套各三轮,分别为 40/40、25/25、24/24。
    • 白盒:关联 planner 的删除键、无备选键、自引用和索引替换测试三轮通过。本地旧 mo-tester 对结果格式及 SHOW CREATE 换行作了隔离副本转换并关闭元数据比较,不将其称为原格式完整 BVT。

    待解决点

    MOTR PR 尚待评审合入。

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

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions