Skip to content

[Bug]: cyclic FK mo-dump restore silently loses parent-update enforcement #30604

Closed as not planned

Description

@Ariznawlll

Description

mo-dump can report a successful backup and restore of a valid two-table cyclic foreign-key schema, yet the restored database permits an update that leaves an orphan reference. Both restored SHOW CREATE TABLE statements still display the foreign keys. The generated script interleaves each table's CREATE and INSERT while foreign_key_checks=0; a direct execution of that sequence reproduces the problem, whereas its -no-data schema-only path does not.

Environment

  • MatrixOne main commit: 75ff736835c3d00d40dc00a0a1d3b11129388e5b
  • mo-dump commit: 324c33dbdc7075cd5d7beaafd1e15c76709dd442
  • Isolated local 1 LOG / 1 TN / 1 CN, SQL port 26011; 2026-10-19

Steps to reproduce

Create a fresh database and valid source rows:

CREATE DATABASE fk_cycle_src;
USE fk_cycle_src;
CREATE TABLE a (id INT PRIMARY KEY, b_id INT);
CREATE TABLE b (id INT PRIMARY KEY, a_id INT);
ALTER TABLE a ADD CONSTRAINT fk_a FOREIGN KEY (b_id) REFERENCES b(id) ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE b ADD CONSTRAINT fk_b FOREIGN KEY (a_id) REFERENCES a(id) ON DELETE RESTRICT ON UPDATE RESTRICT;
INSERT INTO a VALUES (1,NULL);
INSERT INTO b VALUES (2,1);
UPDATE a SET b_id=2 WHERE id=1;

Export the whole database with mo-dump -db fk_cycle_src, change only the database name in its generated SQL, then import into a fresh fk_cycle_restored. The dump emits SET foreign_key_checks=0, creates b with an unresolved reference to a, inserts its row, then creates a with an outgoing reference to b, inserts its row, and re-enables checks.

In separate fresh connections with @@foreign_key_checks=1:

UPDATE fk_cycle_src.b SET id=3 WHERE id=2;
UPDATE fk_cycle_restored.b SET id=3 WHERE id=2;
SELECT a.id,a.b_id,b.id FROM fk_cycle_restored.a a JOIN fk_cycle_restored.b b ON b.a_id=a.id;
SHOW CREATE TABLE fk_cycle_restored.a;
SHOW CREATE TABLE fk_cycle_restored.b;

Actual behavior

  • Source update: ERROR 1217 (23000), as expected.
  • Restored update: succeeds; row becomes a=(1,2), b=(3,1), so a.b_id=2 is orphaned.
  • Both SHOW CREATE TABLE outputs still show ON UPDATE RESTRICT; both information_schema.referential_constraints rows remain.
  • The generated dump ends with MODUMP SUCCESS, and import exits zero.

Expected behavior

Restoration must retain live FK enforcement. The restored update must be rejected and both rows remain unchanged, or the import must explicitly fail; it must not silently create inconsistent data.

Stability and controls

  • Three independent source databases and dumps, each imported into a fresh target: source rejected and restored target accepted the update in 3/3 cases.
  • SQL restore to system tenant and CSV+LOCAL restore to an ordinary tenant showed the same behavior.
  • A single forward-referenced FK without a cycle rejects the same parent-key update after parent creation. Original cyclic schema built with ALTER ADD FK also rejects it. A direct minimal CREATE b; INSERT b; CREATE a; INSERT a sequence matching the dump reproduces the hole without mo-dump.
  • mo-dump -no-data recreates the same cyclic schema; inserting both rows after both CREATE statements and then updating the parent key correctly returns 1217 and preserves (a.b_id,b.id)=(2,2). This isolates the failure to the interleaving of data load and unresolved FK resolution, not just the DDL text or cycle alone.
  • After the unwanted update, the orphan row persists and SELECT 1 still succeeds. Other rejected FK writes in the independent RESTRICT/NO ACTION and self-reference controls leave rows unchanged.

Code analysis

pkg/sql/plan/build_ddl.go handles unresolved parents in the ForwardRefer branch around lines 3923-3929 and collects incoming references around lines 4403-4440. Hypothesis: if the unresolved child already contains data when the later parent CREATE resolves its FK, the live parent-side reference is not fully installed, although the catalog/DDL representation is present. cmd/mo-dump/main.go around lines 209-318 interleaves each table's CREATE and INSERT and temporarily disables FK checks. This root-cause hypothesis needs developer confirmation.

Regression coverage

Add an engine regression comparing CREATE b; INSERT b; CREATE a; INSERT a with CREATE b; CREATE a; INSERT b; INSERT a under disabled checks, and a mo-dump roundtrip that asserts post-restore rejected parent-key UPDATE/DELETE as well as row values and SHOW CREATE TABLE. No repository test or PR was added in this investigation.

Related

Activity

  1. added this to the MO-202610 milestone on Oct 19, 2026
  2. added
    kind/bug
    on Oct 19, 2026
  3. added theissue type on Oct 19, 2026
  4. XuPeng-SH commented on Oct 22, 2026

    @XuPeng-SH
    Contributor

    This report is now tracked together with #30615. The canonical issue retains explicit acceptance cases for the behavior described here. This is an administrative duplicate/consolidation closure only; the underlying defect is not claimed fixed. The original description and discussion remain available on this issue.

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

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions