`plan`/`apply` fails on FK referencing a UNIQUE constraint defined later in the dump: "no unique constraint matching given keys"
Environment
- pgschema version: 1.12.2
- Postgres source version: 15.8
- Mode:
planusing the default embedded plan database (no--plan-host) - Schema size: large (100+ tables), many cross-table foreign keys
Summary
When running pgschema plan against a large schema dumped with pgschema dump, applying the desired-state SQL to the temporary/embedded plan database fails with:
ERROR: there is no unique constraint matching given keys for referenced table "parent_orgs" (SQLSTATE 42830)
This happens when a FOREIGN KEY constraint on one table references a UNIQUE constraint on another table (not the primary key), and in the generated dump file the ALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEY block for the referencing table appears before the ALTER TABLE ... ADD CONSTRAINT ... UNIQUE block for the referenced table. Since plan/apply executes the dump's DDL statements in file order within one session, the FK constraint fails because the unique constraint it depends on doesn't exist yet at that point.
A normal pg_dump output orders constraints so that all UNIQUE/PRIMARY KEY constraints are added before any FOREIGN KEY constraints that depend on them (i.e. it topologically sorts by dependency, not just declaration order). It looks like pgschema's dump/apply does not perform this ordering for non-PK unique constraints referenced by FKs on other tables.
Minimal repro
-- source schema
CREATE TABLE parent_orgs (
id bigint PRIMARY KEY,
external_key uuid NOT NULL
);
CREATE TABLE child_records (
id bigint PRIMARY KEY,
org_external_key uuid NOT NULL
);
ALTER TABLE child_records
ADD CONSTRAINT fk_child_org
FOREIGN KEY (org_external_key) REFERENCES parent_orgs (external_key);
ALTER TABLE parent_orgs
ADD CONSTRAINT parent_orgs_external_key_key UNIQUE (external_key);
pgschema dump --schema public ... > schema.sqlagainst a database with this structure.- Inspect
schema.sql: thefk_child_orgFK constraint block appears before theparent_orgs_external_key_keyUNIQUE constraint block (both are emitted, apparently, in a stable order such as alphabetical-by-constraint-name or per-table order, not a dependency-aware order). pgschema plan --schema public --file schema.sql ...against any target (including a fresh/empty database, using the default embedded plan DB) fails while applying the desired state:
Error: failed to apply desired state: failed to apply schema SQL to temporary schema pgschema_tmp_...: ERROR: there is no unique constraint matching given keys for referenced table "parent_orgs" (SQLSTATE 42830)
Expected behavior
dump/plan/apply should order constraint-creation DDL so that any UNIQUE/PRIMARY KEY constraint referenced by a FOREIGN KEY is created before the FOREIGN KEY that depends on it — regardless of table/constraint name ordering — the same way pg_dump does via topological sorting of dependencies.
Impact
This makes plan unusable on any schema where such a reference pattern exists, which is common in schemas that have foreign keys pointing at a natural/business key (e.g. a UUID) rather than the primary key. It's a hard blocker, not just a cosmetic diff — plan errors out entirely rather than producing a (possibly noisy) diff.