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.61k stars 649 forks source link

[BUG]: drizzle-zod ignores $type in pgTable definition #3544

Open karmo-sisutech opened 1 week ago

karmo-sisutech commented 1 week ago

Report hasn't been filed before.

What version of drizzle-orm are you using?

0.36.1

What version of drizzle-kit are you using?

0.24.2

Other packages

drizzle-zod@0.5.1

Describe the Bug

If I use $type for column in pgTable definition:

export enum Color {
    Blue = 'blue',
    Green = 'green',
}
...
export const carTable = pgTable(
    ...
    color: text().notNull().$type<Color>(),
    ...
);

... and use drizzle-zod to generate insert schema for table:

export const insertCarSchema = createInsertSchema(carTable);

... then I get type error on second line:

const data = insertCarSchema.parse(input);
await db.insert(carTable).values(data);
                                 ^^^^

Error:

Types of property 'color' are incompatible.
        Type 'string' is not assignable to type 'SQL\<unknown> | P...
________
(parameter) data: {
    ...
    color: string;
    ...
}

color after Zod parsing is plain string not desired Color. If I remove $type from pgTable then type error disappears. drizzle-zod should respect $type definition and not give type error.