pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

Bug: Schema with quoted mixed-case FK columns cannot be round-tripped

Open vytautas-karpavicius opened this issue 7 months ago • 0 comments

pgschema version: 1.9.0

Description

A schema with quoted mixed-case column names and circular foreign key dependencies cannot be round-tripped through pgschema dump + pgschema plan. The dump produces a desired state file that pgschema plan refuses to load.

Steps to reproduce

# 0. Start PostgreSQL
docker run --rm -d --name pg15 -e POSTGRES_HOST_AUTH_METHOD=trust -p 5432:5432 postgres:15

# 1. Create databases and load the schema
psql -h localhost -U postgres -c "CREATE DATABASE plandb"
psql -h localhost -U postgres -c "CREATE DATABASE targetdb"
psql -h localhost -U postgres -d targetdb -c "
CREATE TABLE aaa (
    \"aId\" bigint NOT NULL,
    \"bId\" bigint,
    CONSTRAINT aaa_pkey PRIMARY KEY (\"aId\")
);
CREATE TABLE bbb (
    \"bId\" bigint NOT NULL,
    \"aId\" bigint,
    CONSTRAINT bbb_pkey PRIMARY KEY (\"bId\")
);
ALTER TABLE aaa ADD CONSTRAINT aaa_fk FOREIGN KEY (\"bId\") REFERENCES bbb (\"bId\") DEFERRABLE;
ALTER TABLE bbb ADD CONSTRAINT bbb_fk FOREIGN KEY (\"aId\") REFERENCES aaa (\"aId\") DEFERRABLE;
"

# 2. Dump the schema
pgschema dump \
  --host localhost --port 5432 --user postgres \
  --db targetdb --sslmode disable \
  > desired.sql

# 3. Plan against the same database — expects zero drift
pgschema plan \
  --host localhost --port 5432 --user postgres \
  --db targetdb --sslmode disable \
  --plan-host localhost --plan-port 5432 \
  --plan-user postgres --plan-db plandb --plan-sslmode disable \
  --file desired.sql

Expected result

No changes detected.

Actual result

Error: failed to apply desired state: failed to apply schema SQL to temporary schema
pgschema_tmp_...: ERROR: column "bid" referenced in foreign key constraint does not exist (SQLSTATE 42703)

vytautas-karpavicius avatar Apr 29 '26 13:04 vytautas-karpavicius