Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]:drizzle-zod produces wrong type #1110

Open
SooditK opened this issue Aug 24, 2023 · 5 comments · May be fixed by #2220
Open

[BUG]:drizzle-zod produces wrong type #1110

SooditK opened this issue Aug 24, 2023 · 5 comments · May be fixed by #2220
Assignees
Labels
bug Something isn't working drizzle-zod

Comments

@SooditK
Copy link

SooditK commented Aug 24, 2023

What version of drizzle-orm are you using?

0.28.5

What version of drizzle-kit are you using?

0.19.13

Describe the Bug

I'm using drizzle-orm, drizzle-kit & drizzle-zod in my application. One of my schema looks like following:

export const committee = pgTable("committee", {
  id: text("id").primaryKey().notNull(),
  safety_health: text("safety_health"),
  safety_health_sources: text(
    "safety_health_sources"
  ).array(),
  // other properties
}

The schema works fine when I push, safety_health_sources is created as an array (No issues till this point)

I create zod schema as well, like following:

export const committeeSchema = createSelectSchema(committee);

and when I do:

type Committee = Partial<
  z.infer<typeof committeeSchema>
>;
// the type become something like following
type Committee = {
    id?: string | undefined;
    safety_health?: string | null | undefined;
    safety_health_sources?: string | null | undefined; // <--- The problem is here, it should be an Array<string>
    // other properties
}

Expected behavior

The type should be something like:

type Committee = {
    id?: string | undefined;
    safety_health?: string | null | undefined;
    safety_health_sources?: string[] | null | undefined; // <--- This
    // other properties
}

Environment & setup

NodeJS: v20.5.0
npm: 9.8.0
OS: EndeavourOS Linux x86_64

@SooditK SooditK added the bug Something isn't working label Aug 24, 2023
@austinm911
Copy link

I ran into this as well. This fixes it for now

export const selectDataSchema = createSelectSchema(
    responseData,
    {
        text_array: z.array(z.string()),
    },
)

@SooditK
Copy link
Author

SooditK commented Aug 31, 2023

The following produces correct types (It's strange):

type Committee = Partial<InferInsertModel<typeof committee>>;

The type it produces:

type Committee = {
    safety_health?: string | null | undefined;
    safety_health_sources?: string[] | null | undefined; // <--- it works, it produces string[]
    // other properties
}

@CaptainYarb
Copy link

Ran into this exact problem and have a quick replication example here.

Likely related to #1345 as well

@bhuynhdev
Copy link

This is showing up in drizzle-typebox too. Array fields in PostgreSQL is also not converting to Array type correct when calling createSelectSchema. Fortunately, drizzle-typebox have options for manual override.

@SooditK
Copy link
Author

SooditK commented Dec 20, 2023

@dankochetov any updates on this issue 😃

Sukairo-02 added a commit to Sukairo-02/drizzle-orm that referenced this issue Apr 27, 2024
…added related type tests, removed unnecessary or repeating checks
Sukairo-02 added a commit to Sukairo-02/drizzle-orm that referenced this issue Apr 27, 2024
…added related type tests, removed unnecessary or repeating checks
@Sukairo-02 Sukairo-02 linked a pull request Apr 28, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working drizzle-zod
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants