pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

cross schema foreign key constraints introduce errors in planning

Open whoward opened this issue 1 year ago • 2 comments

Given the following database definition:

create schema some_schema;

create table public.some_table(id serial primary key);

create table some_schema.some_other_table(
  id serial primary key,
  some_table_id integer not null references public.some_table(id)
);

pgschema dump --schema some_schema produces the following output:

--
-- pgschema database dump
--

-- Dumped from database version PostgreSQL 17.0
-- Dumped by pgschema version 1.4.0


--
-- Name: some_other_table; Type: TABLE; Schema: -; Owner: -
--

CREATE TABLE IF NOT EXISTS some_other_table (
    id SERIAL,
    some_table_id integer NOT NULL,
    CONSTRAINT some_other_table_pkey PRIMARY KEY (id),
    CONSTRAINT some_other_table_some_table_id_fkey FOREIGN KEY (some_table_id) REFERENCES public.some_table (id)
);

and when you attempt to pgschema plan with this dump file you get the following error:

Error: failed to apply desired state to embedded PostgreSQL: failed to apply schema SQL: ERROR: relation "public.some_table" does not exist (SQLSTATE 42P01)

This makes sense because as of https://github.com/pgschema/pgschema/commit/aa80712a8c2a43238d16265172df275bc6a914e9 we are trying to load dumps into an embedded postgres database, but only one schema at a time which obviously breaks cross-schema references. I'm not sure if there might be an easier way but solving this might require doing dumps and plans across multiple schemas at the same time. Any thoughts?

whoward avatar Oct 23 '25 14:10 whoward

Please use external plan db: https://www.pgschema.com/cli/plan-db. Available in 1.4.1

tianzhou avatar Nov 01 '25 10:11 tianzhou

very nice! i checked and this works well. Thanks!

whoward avatar Nov 04 '25 16:11 whoward