Repository navigation
Description
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
maincommit:75ff736835c3d00d40dc00a0a1d3b11129388e5b mo-dumpcommit: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), soa.b_id=2is orphaned. - Both
SHOW CREATE TABLEoutputs still showON UPDATE RESTRICT; bothinformation_schema.referential_constraintsrows 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 FKalso rejects it. A direct minimalCREATE b; INSERT b; CREATE a; INSERT asequence matching the dump reproduces the hole without mo-dump. mo-dump -no-datarecreates 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 1still 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
- [Bug]: DATA BRANCH CREATE DATABASE rejects valid cyclic foreign-key schemas #26111 concerned DATA BRANCH rejecting cycles before creation; this issue is a successful logical restore that silently loses enforcement.
- [Bug]: CREATE TABLE can attach a stale incoming foreign key to a replacement table #29852 concerned a concurrent DROP/recreate race; this reproducer is single-session and deterministic.
Activity
XuPeng-SH commentedon Oct 22, 2026
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.
fix(sql): keep foreign-key integrity through restore and deferred FK …