Skip to content

[Bug]: CREATE DATABASE CLONE fails after ALTER TABLE on a foreign-key child table #26328

Closed

Description

@gouhongshen

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

main

Commit ID

1ca928aefd

Other Environment Information

  • Reproduced on the latest main commit above.
  • Also reproduced on MatrixOne Cloud 8.0.30-MatrixOne-v4.1.4.
  • The failure is independent of table data and deployment environment.

Actual Behavior

CREATE DATABASE ... CLONE ... fails when the source database contains a foreign-key child table that has been altered after creation:

ERROR 20101 (HY000): internal error: can't find fkTable from fk clone_fk_dst.custom_operator.(source_file_id) {...}

A schema created directly in its final form clones successfully. The equivalent schema produced by creating the foreign key first and then running ALTER TABLE ... ADD COLUMN fails consistently.

Expected Behavior

Database clone should reconstruct the same schema and data regardless of whether the current schema was created directly or reached through valid ALTER TABLE operations.

Steps to Reproduce

DROP DATABASE IF EXISTS clone_fk_src;
DROP DATABASE IF EXISTS clone_fk_dst;

CREATE DATABASE clone_fk_src;
USE clone_fk_src;

CREATE TABLE file (
    file_id VARCHAR(36) NOT NULL,
    PRIMARY KEY (file_id)
);

CREATE TABLE custom_operator (
    id BIGINT NOT NULL AUTO_INCREMENT,
    source_file_id VARCHAR(36) DEFAULT NULL,
    PRIMARY KEY (id),
    CONSTRAINT fk_custom_operator_source_file
        FOREIGN KEY (source_file_id)
        REFERENCES file (file_id)
);

ALTER TABLE custom_operator
    ADD COLUMN kind VARCHAR(32) NOT NULL DEFAULT 'code';

CREATE DATABASE clone_fk_dst CLONE clone_fk_src;

The last statement fails with can't find fkTable from fk clone_fk_dst.custom_operator.(source_file_id).

Control case: define kind in the original CREATE TABLE custom_operator statement and omit the ALTER TABLE; cloning succeeds.

Additional information

Verified observations

  • The issue reproduces without inserting any rows.
  • Cloning the already cloned/upgraded database fails in the same way.
  • A real upgraded schema has custom_operator.rel_version = 4 and fails.
  • A freshly created equivalent schema has custom_operator.rel_version = 0 and succeeds.
  • The error is raised in pkg/sql/plan/build_show_util.go when ctx.ResolveById(fk.ForeignTbl, snapshot) returns no parent table definition while reconstructing the child table DDL.

Suggested repair direction

The clone path must resolve the child table definition, its ForeignTbl reference, and the referenced parent table from one consistent catalog snapshot/version.

Please avoid resolving the parent from the current catalog as a fallback, because that could silently combine schema versions. A clean fix should either:

  1. make snapshot-aware relation-ID resolution correctly handle altered table versions; or
  2. have database clone build and use a snapshot-scoped table-ID-to-definition map for all source tables when reconstructing foreign keys.

A regression test should cover:

  • parent table + foreign-key child table;
  • ALTER TABLE on the child after FK creation;
  • CREATE DATABASE ... CLONE ...;
  • cloned FK points to the cloned parent and remains enforceable.

Activity

  1. self-assigned this
    on Jul 28, 2026
  2. added
    kind/bug
    on Jul 28, 2026
  3. gouhongshen commented on Jul 28, 2026

    @gouhongshen
    ContributorAuthor

    Root cause is now confirmed and the fix is in draft PR #26330.

    The earlier snapshot-resolution hypothesis was downstream of the actual metadata corruption:

    1. ALTER TABLE COPY mode creates a replacement relation with a new physical table ID.
    2. The source table's mo_foreign_keys rows are removed when the source is dropped, and the copy table's rows were then deleted instead of renamed to the original table name.
    3. The parent relation's RefChildTableDef removed the old child ID but did not add the replacement child ID.
    4. Database clone therefore lost the FK dependency used for topological ordering and could reconstruct the child before the parent, producing can't find fkTable from fk ....

    The PR preserves both metadata representations, handles the self-reference sentinel, and adds manually audited ALTER/clone regression coverage.

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

Metadata

Metadata

Assignees

Labels

kind/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions