drizzle-team / drizzle-team/drizzle-orm

Partial unique index migration does not generate valid where clause

Open
#3,349 8 comments 24 reactions 1 assignee Claimed by @AndriiSherman View on GitHub
drizzle/kit improvement priority
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

The following schema does not generate a valid migration. The value for the where clause is not substituted in the generated SQL.

```ts
export const invitationLinks = pgTable(
"invitation_links",
{
id: uuid("id").primaryKey().defaultRandom(),
workspaceId: uuid("workspace_id")
.notNull()
.references(() => workspaces.id, {
onDelete: "cascade",
}),
active: boolean("active").notNull().default(true),
createdAt: timestamp("created_at").notNull().defaultNow(),
},
(table) => ({
// We need to make sure that we don't have multiple active invitation links
// for the same workspace.
uniqueIndex: uniqueIndex()
.on(table.workspaceId, table.active)
.where(eq(table.active, true)),
}),
);
```

## Expected Output

```sql
--> [...]
CREATE UNIQUE INDEX IF NOT EXISTS "invitation_links_workspace_id_active_index" ON "invitation_links" USING btree ("workspace_id","active") WHERE "invitation_links"."active" = TRUE;
```

## Actual Output

```sql
--> [...]
CREATE UNIQUE INDEX IF NOT EXISTS "invitation_links_workspace_id_active_index" ON "invitation_links" USING btree ("workspace_id","active") WHERE "invitation_links"."active" = $1;
```

## Error

```
applying migrations...PostgresError: there is no parameter $1
at ErrorResponse (/Users/paulo/Projects/tasks_app/node_modules/.pnpm/drizzle-kit@0.22.7/node_modules/drizzle-kit/bin.cjs:79677:27)
at handle (/Users/paulo/Projects/tasks_app/node_modules/.pnpm/drizzle-kit@0.22.7/node_modules/drizzle-kit/bin.cjs:79454:7)
at Socket.data (/Users/paulo/Projects/tasks_app/node_modules/.pnpm/drizzle-kit@0.22.7/node_modules/drizzle-kit/bin.cjs:79277:9)
at Socket.emit (node:events:519:28)
at addChunk (node:internal/streams/readable:559:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
at Readable.push (node:internal/streams/readable:390:5)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
at TCP.callbackTrampoline (node:internal/async_hooks:130:17) {
severity_local: 'ERROR',
severity: 'ERROR',
code: '42P02',
position: '177',
file: 'parse_expr.c',
line: '864',
routine: 'transformParamRef'
}
```

Versions

drizzle-orm 0.31.2
drizzle-kit 0.22.7
postgres 3.4.4

Node v21.7.3
Pnpm 9.2.0

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.