pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

Changing a column's type fails at apply when a view reads the column

Open jayhemnani9910 opened this issue 2 weeks ago • 0 comments

Widening a column that any view reads fails at apply:

-- current database
CREATE TABLE orders (id bigserial PRIMARY KEY, total numeric(10,2) NOT NULL);
CREATE VIEW order_totals AS SELECT sum(total) AS total FROM orders;

-- desired.sql: the same, with numeric(12,2)
$ pgschema plan --file desired.sql --output-sql stdout
ALTER TABLE orders ALTER COLUMN total TYPE numeric(12,2);

$ pgschema apply --file desired.sql --auto-approve
Error: failed to execute concatenated statements in group 1: ERROR: cannot alter type of a column used by a view or rule (SQLSTATE 0A000)

The same happens for varchar(50) -> varchar(100), for a join view, and when the view text also changes. In a larger migration, the groups before this one are already committed when it fails.

Views are dropped ahead of table changes only by generatePreDropRecreatedRegularViewsSQL (internal/diff/diff.go), and only for dropped tables or dropped columns (#444), and only for views already in modifiedViews with RequiresRecreate. A column type change isn't counted, and a view whose text doesn't change is never in modifiedViews, so nothing drops order_totals before the ALTER COLUMN ... TYPE.

Expected: drop the views that depend on the changed column (and their dependents), alter the column, then recreate them, as already done for dropped columns.

Built from main 6377a12 (v1.14.1 has the same logic), against Postgres 16.

jayhemnani9910 avatar Nov 14 '26 05:11 jayhemnani9910