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
23.84k stars 592 forks source link

[BUG]:drizzle-zod doesn't accept custom id value #2957

Open patolit opened 1 month ago

patolit commented 1 month ago

What version of drizzle-orm are you using?

0.33.3

What version of drizzle-kit are you using?

0.22.7

Describe the Bug

I have a table that historically had been defined with custom Id when we created drizzle we did it with the following value:

export const marketingSmsMessages = mysqlTable( 'marketing_sms_messages', { id: varchar('id', { length: 36 }).notNull(),

When trying to run: const insertMarketingSmsMessageSchema = createInsertSchema(marketingSmsMessages)

get the following error: Argument of type 'MySqlTableWithColumns<{ name: "marketing_sms_messages"; schema: undefined; columns: { id: MySqlColumn<{ name: "id"; tableName: "marketing_sms_messages"; dataType: "string"; columnType: "MySqlVarChar"; data: string; driverParam: string | number; notNull: true; hasDefault: false; enumValues: [...]; baseColumn: never; ...' is not assignable to parameter of type 'Table<TableConfig<Column<any, object, object>>>'. The types of '_.config.columns' are incompatible between these types. Type '{ id: MySqlColumn<{ name: "id"; tableName: "marketing_sms_messages"; dataType: "string"; columnType: "MySqlVarChar"; data: string; driverParam: string | number; notNull: true; hasDefault: false; enumValues: [...]; baseColumn: never; }, object>; ... 16 more ...; updatedAt: MySqlColumn<...>; }' is not assignable to type 'Record<string, Column<any, object, object>>'. Property 'id' is incompatible with index signature. Type 'MySqlColumn<{ name: "id"; tableName: "marketing_sms_messages"; dataType: "string"; columnType: "MySqlVarChar"; data: string; driverParam: string | number; notNull: true; hasDefault: false; enumValues: [...]; baseColumn: never; }, object>' is missing the following properties from type 'Column<any, object, object>': generated, generatedIdentity

Expected behavior

I expected the insert schema to just parse the schema in use and give a 1 to 1 zod schema. as without it I need to manually create zod schema for each table

Environment & setup

No response

jadnast commented 1 month ago

Here you can find more information

It appears that Drizzle ORM's createInsertSchema isn't recognizing the custom id column in your schema automatically. This is likely due to Drizzle ORM's Zod integration not handling non-standard or custom ID fields out of the box.

To resolve this, you can manually specify the schema for the custom id column. This approach ensures the validation for the id field is handled correctly.

Here’s a quick example to illustrate:

import { z } from 'zod';
import { createInsertSchema } from 'drizzle-orm/zod';

const insertMarketingSmsMessageSchema = createInsertSchema(marketingSmsMessages, {
  id: z.string().length(36)  // explicitly defining the custom ID field as a string of length 36
});

By explicitly defining the id field with the correct Zod schema, you're providing the necessary context for validation. In this example, we're defining the id as a string of length 36 (assuming it's a UUID or a similar identifier), which resolves the issue of Drizzle ORM not handling this column automatically.

Let me know if this helps or if you have any further questions!

patolit commented 1 month ago

Nikita, thank you for your prompt response.

although this works for the zod definition it still shows an ugly TS error, that to avoid I need to comment on the code line with @.*** to overcome


Type '{ id: MySqlColumn<{ name: "id"; tableName: "marketing_sms_messages";
dataType: "string"; columnType: "MySqlVarChar"; data: string; driverParam:
string | number; notNull: true; hasDefault: false; enumValues: [...];
baseColumn: never; }, object>; ... 16 more ...; updatedAt:
MySqlColumn<...>; }' is not assignable to type 'Record<string, Column<any,
object, object>>'.
Property id is incompatible with index signature.```

On Fri, Sep 13, 2024 at 2:07 AM Nikita Kononenko ***@***.***>
wrote:

> Here you can find more information <https://orm.drizzle.team/docs/zod>
>
> It appears that Drizzle ORM's createInsertSchema isn't recognizing the
> custom id column in your schema automatically. This is likely due to
> Drizzle ORM's Zod integration not handling non-standard or custom ID fields
> out of the box.
>
> To resolve this, you can manually specify the schema for the custom id
> column. This approach ensures the validation for the id field is handled
> correctly.
>
> Here’s a quick example to illustrate:
>
> import { z } from 'zod';import { createInsertSchema } from 'drizzle-orm/zod';
> const insertMarketingSmsMessageSchema = createInsertSchema(marketingSmsMessages, {
>   id: z.string().length(36)  // explicitly defining the custom ID field as a string of length 36});
>
> By explicitly defining the id field with the correct Zod schema, you're
> providing the necessary context for validation. In this example, we're
> defining the id as a string of length 36 (assuming it's a UUID or a
> similar identifier), which resolves the issue of Drizzle ORM not handling
> this column automatically.
>
> *Let me know if this helps or if you have any further questions!*
>
> —
> Reply to this email directly, view it on GitHub
> <https://github.com/drizzle-team/drizzle-orm/issues/2957#issuecomment-2347388445>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AI57YKIW5HILEAENCEZU3WDZWINEFAVCNFSM6AAAAABOC5ZJN6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNBXGM4DQNBUGU>
> .
> You are receiving this because you authored the thread.Message ID:
> ***@***.***>
>