Table dependency not resolving
Hello, I have the following:
src/public/functions/accept_extracted_recipes.sql:
CREATE OR REPLACE FUNCTION accept_extracted_recipes(
recipe_ids uuid[]
)
RETURNS SETOF recipe_user_save
LANGUAGE plpgsql
VOLATILE
AS $$
BEGIN
-- ... function content omitted
END;
$$;
and
src/public/tables/recipe_user_save.sql:
CREATE TABLE IF NOT EXISTS recipe_user_save (
recipe_id uuid,
user_id uuid DEFAULT auth.uid(),
CONSTRAINT recipe_user_save_pkey PRIMARY KEY (recipe_id, user_id),
CONSTRAINT recipe_user_save_recipe_id_fkey FOREIGN KEY (recipe_id) REFERENCES recipe (id) ON DELETE CASCADE
);
When I run the following:
../pgschema/pgschema plan \
--host localhost \
--user postgres \
--db postgres \
--password postgres \
--port 54322 \
--plan-host localhost \
--plan-db postgres \
--plan-user postgres \
--plan-password postgres \
--plan-port 54322 \
--file src/public/main.sql \
--output-human \
--output-json plan.json \
--output-sql plan.sql --schema public
I get:
Error: failed to apply desired state: failed to apply schema SQL to temporary schema pgschema_tmp_20260414_142719_c0de6c1b: ERROR: type "recipe_user_save" does not exist (SQLSTATE 42704)
Even when I empty the function - isolating the problem to RETURNS SETOF recipe_user_save.
I've tried marking it explicitly as RETURNS SETOF public.recipe_user_save and the table defintion with public but nothing changes.
Any ideas here?
Thanks for the report — this is a real ordering issue, and I was able to reproduce it.
Root cause
It has nothing to do with schema qualification (public.). The cause is statement order in your desired-state SQL.
When plan builds the desired state, pgschema concatenates your SQL files in include order (the order of your \i directives in main.sql) and applies that as-is to a temporary schema. RETURNS SETOF recipe_user_save makes the function depend on the table's row-type, and PostgreSQL resolves a function's return type at creation time — so if the function is applied before the table exists, you get:
ERROR: type "recipe_user_save" does not exist (SQLSTATE 42704)
(public. didn't help because pgschema strips the target-schema prefix internally, and qualification was never the problem.)
Workaround
Order your \i includes in main.sql so that tables come before the functions that reference them. For example:
\i tables/recipe_user_save.sql
-- ... other tables ...
\i functions/accept_extracted_recipes.sql
Including the tables/ directory before functions/ resolves it. I verified that table-before-function applies cleanly while function-before-table fails with exactly your error.
Going forward
This will recur for any composite-type dependency — RETURNS SETOF <table>, %ROWTYPE, table-typed arguments, domains used in tables, etc. I'm tracking a follow-up to make desired-state application dependency-tolerant (so include order won't matter), but for now ordering your includes is the fix.