drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
21.44k stars 484 forks source link

[BUG]: drizzle-kit generate for unique index produces incorrect migration #2506

Open dmmulroy opened 2 weeks ago

dmmulroy commented 2 weeks ago

What version of drizzle-orm are you using?

0.31.1

What version of drizzle-kit are you using?

0.22.2

Describe the Bug

export const credential = pgTable(
    'credential',
    {
        id: serial('id').primaryKey(),
        userId: integer('user_id')
            .notNull()
            .references(() => user.id),
        active: boolean('active').notNull(),
        hash: text('hash').notNull(),
        createdAt: timestamp('created_at', { withTimezone: true, mode: 'date' }).defaultNow(),
        updatedAt: timestamp('updated_at', { withTimezone: true, mode: 'date' })
            .defaultNow()
            .$onUpdate(() => new Date())
    },
    (table) => {
                // Add this constraint as a migration
        return {
            activeIdx: uniqueIndex('active_credential_idx')
                .on(table.userId, table.active)
                .where(eq(table.active, true))
        };
    }
);

This produces the following migration as a prepared statement using $1

DROP INDEX IF EXISTS "active_credential_idx";--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "active_credential_idx" ON "credential" USING btree (user_id,active) WHERE "credential"."active" = $1;

Expected behavior

It should produce a migration file like this:

DROP INDEX IF EXISTS "active_credential_idx";--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "active_credential_idx" ON "credential" USING btree (user_id,active) WHERE "credential"."active" = true;

Environment & setup

No response

dmmulroy commented 2 weeks ago

Looks like it happens for more than just boolean columns per #2508