Skip to content

[BUG]: drizzle-kit generate doesn't drop/recreate dependent views when altering column types #6194

Description

@machadinhos

Report hasn't been filed before.

  • I have verified that the bug I'm about to 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!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions