You export a database, try to load it into a fresh instance, and MySQL throws:
Or, on PostgreSQL:
DETAIL: Key (user_id)=(42) is not present in table "users".
Why this happens
A foreign key says “every orders.user_id must point to an existing users.id.” When the dump inserts orders before users, the referenced parent row doesn't exist yet, and the database rejects the row.
Most dump tools write tables alphabetically or in creation order — not in foreign key dependency order. So orders (child) frequently lands before users (parent), and the import breaks.
Fix 1: Reorder INSERTs by dependency (the correct fix)
The clean solution is to insert every parent table before its children. That means resolving the foreign keys into a dependency graph and loading tables in topological order: users → orders → order_items.
Doing this by hand in a multi-gigabyte file is painful — you have to trace every FOREIGN KEY across CREATE TABLE and ALTER TABLE statements and cut/paste huge INSERT blocks in the right sequence.
Fix 2: Disable foreign key checks during import
For a complete, already-consistent dump you can safely skip validation while loading. On MySQL, wrap the file:
-- MySQL SET FOREIGN_KEY_CHECKS = 0; -- ... all your INSERT statements ... SET FOREIGN_KEY_CHECKS = 1;
On PostgreSQL, defer the constraints inside a transaction:
-- PostgreSQL BEGIN; SET CONSTRAINTS ALL DEFERRED; -- ... all your INSERT statements ... COMMIT;
Re-enable checks afterwards so future writes stay validated. This works, but it only masks the ordering problem — if the dump is partial, you can still end up with orphaned rows.
Fix 3: Do both automatically with DumpCleaner
DumpCleaner reads the dump, extracts every foreign key from CREATE TABLE and ALTER TABLE, builds the dependency graph, and exports a clean file that just imports:
- Drag your
.sqldump into DumpCleaner (MySQL, MariaDB, PostgreSQL, SQLite or MS SQL). - Choose INSERT order → By dependencies (FK-aware). Parents are placed before children automatically via topological sort.
- Optionally tick Disable foreign key checks — it wraps the output with the correct statement for your database.
- Export. Import the new file — the constraint error is gone.
It even detects circular foreign keys and warns you instead of producing an impossible order.
No more manual INSERT surgery
DumpCleaner reorders inserts by foreign key dependency on files of any size — streaming, with constant memory. Native macOS & iPadOS app, one-time purchase.
Download on the App StoreFrequently asked questions
Why does “a foreign key constraint fails” happen on import?
A child row references a parent row that hasn't been inserted yet, because the dump orders INSERTs alphabetically or by creation time rather than by foreign key dependency.
Is it safe to disable foreign key checks during import?
Yes, for a full and consistent dump. You're only skipping validation of data that was already valid in the source database. Re-enable checks afterwards.
How do I reorder INSERTs without editing the file by hand?
DumpCleaner detects foreign keys, builds a dependency graph, and exports the INSERTs in topological order — so parents always load before children.
Does this work for PostgreSQL too?
Yes. DumpCleaner supports pg_dump output and can emit SET CONSTRAINTS ALL DEFERRED as well as dependency-ordered inserts.
Import dumps without the FK headache
Drag, reorder by dependencies, export. DumpCleaner handles the foreign keys so your import just works.
Download on the App Store