Report hasn't been filed before.
What version of drizzle-orm are you using?
1.0.0-rc.4
What version of drizzle-kit are you using?
1.0.0-rc.4
Other packages
No response
Describe the Bug
Hi!
I've noticed that when altering columns on tables that are used by views, drizzle-kit does not automatically wrap the column alterations with view drops and recreations. This leads to migration execution failures because the database blocks altering column types for any table referenced by a view (PostgresError: cannot alter type of a column used by a view or rule).
Minimal Reproducible Example
Initial Schema:
import { sql } from "drizzle-orm";
import { integer, pgTable, pgView, timestamp } from "drizzle-orm/pg-core";
export const accessLogsTable = pgTable("access_logs", {
id: integer("id").primaryKey().generatedAlwaysAsIdentity(),
accessedAt: timestamp("accessed_at", { mode: "string" }).notNull().defaultNow(),
});
export const evenAccessLogsView = pgView("even_access_logs").as((qb) =>
qb.select().from(accessLogsTable).where(sql`${accessLogsTable.id} % 2 = 0`)
);
Updated Schema:
import { sql } from "drizzle-orm";
import { integer, pgTable, pgView, timestamp } from "drizzle-orm/pg-core";
export const accessLogsTable = pgTable("access_logs", {
id: integer("id").primaryKey().generatedAlwaysAsIdentity(),
accessedAt: timestamp("accessed_at", { mode: "string", withTimezone: true }).notNull().defaultNow(),
});
export const evenAccessLogsView = pgView("even_access_logs").as((qb) =>
qb.select().from(accessLogsTable).where(sql`${accessLogsTable.id} % 2 = 0`)
);
Generated Migration
drizzle-kit generate produces the following migration:
ALTER TABLE "access_logs" ALTER COLUMN "accessed_at" SET DATA TYPE timestamp with time zone USING "accessed_at"::timestamp with time zone;
Expected Behavior
Because even_access_logs depends on accessLogsTable, the migration should drop the view before altering the column and recreate it afterward:
DROP VIEW IF EXISTS "even_access_logs";
ALTER TABLE "access_logs" ALTER COLUMN "accessed_at" SET DATA TYPE timestamp with time zone USING "accessed_at"::timestamp with time zone;
CREATE VIEW "even_access_logs" AS ...;
Actual Behavior
The migration runs the ALTER TABLE statement directly, resulting in the following database error:
PostgresError: cannot alter type of a column used by a view or rule
When adding new columns to a table, drizzle-kit already handles dropping, updating, and recreating dependent views. Applying this same view-recreation lifecycle to ALTER COLUMN actions would prevent failed migrations.
Note: I reproduced and verified this issue using PostgreSQL, but it is likely an issue with other database vendors (such as MySQL) as well, given how view dependencies are handled during table alterations.
Thank you!
Report hasn't been filed before.
What version of
drizzle-ormare you using?1.0.0-rc.4
What version of
drizzle-kitare you using?1.0.0-rc.4
Other packages
No response
Describe the Bug
Hi!
I've noticed that when altering columns on tables that are used by views,
drizzle-kitdoes not automatically wrap the column alterations with view drops and recreations. This leads to migration execution failures because the database blocks altering column types for any table referenced by a view (PostgresError: cannot alter type of a column used by a view or rule).Minimal Reproducible Example
Initial Schema:
Updated Schema:
Generated Migration
drizzle-kit generateproduces the following migration:Expected Behavior
Because
even_access_logsdepends onaccessLogsTable, the migration should drop the view before altering the column and recreate it afterward:Actual Behavior
The migration runs the
ALTER TABLEstatement directly, resulting in the following database error:When adding new columns to a table,
drizzle-kitalready handles dropping, updating, and recreating dependent views. Applying this same view-recreation lifecycle toALTER COLUMNactions would prevent failed migrations.Note: I reproduced and verified this issue using PostgreSQL, but it is likely an issue with other database vendors (such as MySQL) as well, given how view dependencies are handled during table alterations.
Thank you!