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

Build fails when enabling extendedWhereUnique Prisma preview feature #91

Closed
sethmiller opened this issue Mar 1, 2023 · 5 comments
Closed
Assignees
Labels
bug Something isn't working enhancement New feature or request

Comments

@sethmiller
Copy link

I looked in the issues list and didn't find a duplicate of this issue.

Issue

Enabling the extendedWhereUnique preview feature causes the generated code to fail to build. As best as I can tell it is because the *WhereUniqueInput types are now wrapped in Prisma.AtLeast which changes the type of unique fields and in some cases makes them non-optional.

The error is

prisma/generated/zod/index.ts:93:14 - error TS2322: Type 'ZodObject<{ id: ZodOptional<ZodNumber>; email: ZodOptional<ZodString>; AND: ZodOptional<ZodUnion<[ZodLazy<ZodType<UserWhereInput, ZodTypeDef, UserWhereInput>>, ZodArray<...>]>>; OR: ZodOptional<...>; NOT: ZodOptional<...>; name: ZodNullable<...>; num: ZodOptional<...>; }, "strict", ZodTypeAny, { ...; }, { ...; }>' is not assignable to type 'ZodType<UserWhereUniqueInput, ZodTypeDef, UserWhereUniqueInput>'.
  Types of property '_type' are incompatible.
    Type '{ id?: number | undefined; email?: string | undefined; name?: string | StringNullableFilter | null | undefined; num?: string | number | { d: number[]; e: number; s: number; } | DecimalFilter | undefined; AND?: UserWhereInput | ... 1 more ... | undefined; OR?: UserWhereInput[] | undefined; NOT?: UserWhereInput | ... ...' is not assignable to type 'UserWhereUniqueInput'.
      Type '{ id?: number | undefined; email?: string | undefined; name?: string | StringNullableFilter | null | undefined; num?: string | number | { d: number[]; e: number; s: number; } | DecimalFilter | undefined; AND?: UserWhereInput | ... 1 more ... | undefined; OR?: UserWhereInput[] | undefined; NOT?: UserWhereInput | ... ...' is not assignable to type '{ id: string | number; email: string | number; } & { id?: number | undefined; email?: string | undefined; AND?: Enumerable<UserWhereInput> | undefined; OR?: Enumerable<...> | undefined; NOT?: Enumerable<...> | undefined; name?: string | ... 2 more ... | undefined; num?: string | ... 4 more ... | undefined; }'.
        Type '{ id?: number | undefined; email?: string | undefined; name?: string | StringNullableFilter | null | undefined; num?: string | number | { d: number[]; e: number; s: number; } | DecimalFilter | undefined; AND?: UserWhereInput | ... 1 more ... | undefined; OR?: UserWhereInput[] | undefined; NOT?: UserWhereInput | ... ...' is not assignable to type '{ id: string | number; email: string | number; }'.
          Types of property 'id' are incompatible.
            Type 'number | undefined' is not assignable to type 'string | number'.
              Type 'undefined' is not assignable to type 'string | number'.

93 export const UserWhereUniqueInputSchema: z.ZodType<Prisma.UserWhereUniqueInput> = z.object({

Reproduction

I made a repo to reproduce the issue.

For convenience, the schema.prisma file is

generator client {
  provider = "prisma-client-js"
  previewFeatures = ["extendedWhereUnique"]
}

generator zod {
  provider = "zod-prisma-types"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
  num   Decimal
}
@chrishoermann
Copy link
Owner

@sethmiller thanks for pointing this out. I wasn't aware of this feature and didn't have time to look into this. since it is a preview feature it's not top priority on my list (unlike #90) so it could take a while til I can address this issue.

@sethmiller
Copy link
Author

That makes sense. Prisma does some pretty advanced stuff their Typescript types so I could see this being a pain.

The folks (folk?) over at prisma-zod-generator added support for extendedWhereUnique with this PR. That could provide some inspiration but, for all I know, their implementation might be too different for it to be of any use.

Thanks for looking into this!

@chrishoermann chrishoermann self-assigned this Mar 12, 2023
@chrishoermann chrishoermann added bug Something isn't working enhancement New feature or request labels Mar 12, 2023
@chrishoermann
Copy link
Owner

@sethmiller the extendedWhereUnique option should work now in the latest release. I don't know if I quite nailed it when there are three or more unique fields in a model but at least typescript does not complain and prisma does not complain when providing a parsed schema. So I think this should work now as intended.

@sethmiller
Copy link
Author

I just pulled down the latest version and it successfully generated against our project. We have a table with more than 3 unique constraints. It looks ok to me but I haven't had a chance to thoroughly test it yet.

Thanks again for the quick fix!

@chrishoermann
Copy link
Owner

I checked the prisma generated types again and it seems that the union in the generated schema can be optimized a bit but it should work. I'll close this issue for now. Please reopen when new problems arise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
Development

No branches or pull requests

2 participants