pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

Views are not dependency-ordered

Open KrisBraun opened this issue 9 months ago • 0 comments

Thanks for fixing functions-before-tables ordering in v1.7.2 — that works great now.

However, view-to-view dependencies within a schema are still ordered alphabetically. This causes failures when a view depends on another view that sorts later in the alphabet.

Reproduction

Given these views in the same schema:

-- item_summary.sql
CREATE VIEW item_summary AS
  SELECT ... FROM membership m JOIN item i ON ...;

-- dashboard.sql (depends on item_summary)
CREATE VIEW dashboard AS
  SELECT ... FROM item_summary s JOIN ...;

pgschema emits CREATE VIEW dashboard before CREATE VIEW item_summary because "dashboard" < "item_summary" alphabetically. This fails with:

ERROR: relation "item_summary" does not exist

Impact

When a foundational view has a name that sorts after the views that depend on it, there's no naming workaround. In our schema, several views depend on a single base view whose name sorts after all of them alphabetically.

Current workaround

We split the schema into multiple sub-phases and run pgschema apply multiple times for one schema:

  1. Functions + the foundational view only
  2. + intermediate dependent views
  3. Full schema

This works but defeats the purpose of declarative schema management.

Expected behavior

pgschema should resolve view dependencies (via pg_depend or by parsing FROM clauses) and emit CREATE VIEW statements in dependency order.

KrisBraun avatar Feb 20 '26 20:02 KrisBraun