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
24.78k stars 656 forks source link

[BUG]: `sql.placeholder` throws type error for enums #1608

Open pingustar opened 12 months ago

pingustar commented 12 months ago

What version of drizzle-orm are you using?

0.29.1

What version of drizzle-kit are you using?

0.20.6

Describe the Bug

sql.placeholder throws type error for enums.

import { drizzle } from "drizzle-orm/mysql2"
import { mysqlTable, varchar, mysqlEnum } from "drizzle-orm/mysql-core"
import { sql, SQL } from "drizzle-orm"

const statuses = ["PENDING", "ACCEPTED", "DECLINED"] as const

const status = mysqlEnum("status", statuses)
      .notNull()
      .default("PENDING")

type Status = typeof status

const tableA = mysqlTable("table_a", {
  id: varchar("id", { length: 30}).primaryKey(),
  status: status
});

const db = drizzle({} as any)

db.update(tableA).set({ status: sql.placeholder("status") })

the last line, db.update(tableA).set({ status: sql.placeholder("status") }) throws a type error and a workaround is to type cast like so:

db.update(tableA).set({ status: sql.placeholder("status") as unknown as SQL<string> })

for reference, @Angelelz asked me to open an issue for it in this discord discussion https://discord.com/channels/1043890932593987624/1181229913798357032

Expected behavior

handle enum types correctly.

In general, it would be awesome to get actual type safety with prepared statements. I love the performance improvements, but the missing type safety really bums me out and makes the dev experience very error prone.

Environment & setup

macos bun next 14

alexmondaini commented 6 months ago

I am having same problem with postgres.

https://github.com/drizzle-team/drizzle-orm/discussions/2373