Repository navigation
Conversation
In data only mode with an existing target schema, fetch-pgsql-catalog collects foreign keys of tables outside of the load selection that reference a loaded table into index-fk-deps. drop-pgsql-fkeys drops them, but create-pgsql-fkeys used :do instead of :append for the inner loop, so the generated statements were discarded and those foreign keys were silently lost after the load. Collect the statements, and skip fkeys already created from the table fkey list (fkeys between two loaded tables are listed in both places).
RegWin32
force-pushed
the
fix/recreate-dependent-fkeys
branch
from
November 2, 2026 12:40
9e620d5 to
f89d4af
Compare
RegWin32
marked this pull request as ready for review
November 2, 2026 12:42
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When loading
WITH data only, truncateinto an existing schema withINCLUDING ONLY TABLE NAMES LIKE …, foreign keys that live on a table outside of the selection but reference a selected table are dropped before the load and never created again.Example:
orders.customer_id → customers.id, onlycustomersis in the load. After the run, the foreign key onordersis gone. Foreign keys between two selected tables are created again as expected.Seen with pgloader 3.6.9 (Debian bookworm), MSSQL → PostgreSQL. The code is the same on current
main.Cause
fetch-pgsql-catalog→list-missing-fk-depscollects those foreign keys intoindex-fk-depsof the referenced index.drop-pgsql-fkeysdrops them (:append … :collect), butcreate-pgsql-fkeysused:doinstead of:appendfor the inner loop, so the generatedALTER TABLE … ADD CONSTRAINTstatements were discarded. They only showed up in the debug log asEXTRA FK DEPS!.Fix
index-fk-depsincreate-pgsql-fkeys.list-all-fkeysalso pushes foreign keys between two selected tables intoindex-fk-deps, so skip those whose OID is already in a table fkey list (and dedupe across indexes) to avoidconstraint already existserrors.Note
The v4 (Clojure) implementation may have the same gap; I have not checked it.