How do you find orphaned child records after a migration?
foreign_key_check reports foreign-key violations, identifying child rows and their referenced tables for a migration audit.
SQLite example and interpretation
This in-memory fixture simulates an orphan already present in a legacy database. The check identifies the broken reference. Its business repair is a separate decision.
PRAGMA foreign_keys=OFF;
CREATE TABLE p(id INTEGER PRIMARY KEY);
CREATE TABLE c(pid REFERENCES p(id));
INSERT INTO c VALUES(99);
PRAGMA foreign_key_check;Result [["c", 1, "p", 0]]
中文
如何在迁移之后列出孤立的子记录?
foreign_key_check 返回违反外键关系的记录,可在迁移审计中定位子表、行号和目标表。
SQLite 示例与适用条件
这个内存示例模拟旧库里已有的孤儿记录。检查结果能指明问题位置,如何补齐父记录仍需业务决定。
PRAGMA foreign_keys=OFF;
CREATE TABLE p(id INTEGER PRIMARY KEY);
CREATE TABLE c(pid REFERENCES p(id));
INSERT INTO c VALUES(99);
PRAGMA foreign_key_check;结果 [["c", 1, "p", 0]]