pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

foreign key constraints which reference a table in another schema are not fully qualified in dump

Open whoward opened this issue 1 year ago • 1 comments

The following database definition illustrates an example of a cross-schema foreign key constraint:

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)
);

When pgschema dump --database some_database --schema some_schema is executed we get 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 some_table (id)
);

We can see from the output that the SQL dump does not correctly reconstruct some_other_table in all cases because the foreign key reference is pointing at some_table and not public.some_table. Depending on how the schema search path is set up this might possibly work, but I think we should fully qualify the reference regardless as pgschema plan will continually generate a plan to replace this constraint.

whoward avatar Oct 16 '25 17:10 whoward

this works great, thanks!!

whoward avatar Oct 19 '25 13:10 whoward