omar-dulaimi / prisma-zod-generator

Prisma 2+ generator to emit Zod schemas from your Prisma schema
MIT License
513 stars 45 forks source link

Type error in a self-relation schema #85

Closed lightbluepoppy closed 1 year ago

lightbluepoppy commented 1 year ago

Bug description

There are multiple type errors in my generated schema file TaskCreateOrConnectWithoutSubtasksInput.schema.ts.

I think that this error occurs because this is a self-reference.

❯ npm run build

> foo@0.1.0 build
> next build
- info Linting and checking validity of types ..Failed to compile.

./prisma/generated/schemas/objects/TaskCreateOrConnectWithoutSubtasksInput.schema.ts:8:7
Type error: Type 'ZodObject<{ where: ZodLazy<ZodType<TaskWhereUniqueInput, ZodTypeDef, TaskWhereUniqueInput>>; create: ZodUnion<...>; }, "strict", ZodTypeAny, { ...; }, { ...; }>' is not assignable to type 'ZodType<TaskCreateOrConnectWithoutSubtasksInput, ZodTypeDef, TaskCreateOrConnectWithoutSubtasksInput>'.
  The types of '_type.create' are incompatible between these types.
    Type '(TaskCreateWithoutSubtasksInput | TaskUncheckedCreateWithoutSubtasksInput) & (TaskCreateWithoutSubtasksInput | TaskUncheckedCreateWithoutSubtasksInput | undefined)' is not assignable to type '(Without<TaskCreateWithoutSubtasksInput, TaskUncheckedCreateWithoutSubtasksInput> & TaskUncheckedCreateWithoutSubtasksInput) | (Without<...> & TaskCreateWithoutSubtasksInput)'.
      Type 'TaskCreateWithoutSubtasksInput & TaskUncheckedCreateWithoutSubtasksInput' is not assignable to type '(Without<TaskCreateWithoutSubtasksInput, TaskUncheckedCreateWithoutSubtasksInput> & TaskUncheckedCreateWithoutSubtasksInput) | (Without<...> & TaskCreateWithoutSubtasksInput)'.
        Type 'TaskCreateWithoutSubtasksInput & TaskUncheckedCreateWithoutSubtasksInput' is not assignable to type 'Without<TaskUncheckedCreateWithoutSubtasksInput, TaskCreateWithoutSubtasksInput> & TaskCreateWithoutSubtasksInput'.
          Type 'TaskCreateWithoutSubtasksInput & TaskUncheckedCreateWithoutSubtasksInput' is not assignable to type 'Without<TaskUncheckedCreateWithoutSubtasksInput, TaskCreateWithoutSubtasksInput>'.
            Types of property 'userId' are incompatible.
              Type 'string' is not assignable to type 'undefined'.

   6 | import type { Prisma } from '@prisma/client'
   7 | 
>  8 | const Schema: z.ZodType<Prisma.TaskCreateOrConnectWithoutSubtasksInput> =
     |       ^
   9 |   z
  10 |     .object({
  11 |       where: z.lazy(() => TaskWhereUniqueInputObjectSchema),

How to reproduce

I am not sure. I can help if someone give me any instruction.

Expected behavior

No response

Prisma information

model Task {
  id          String    @id @default(cuid())
  title       String
  user        User      @relation(fields: [userId], references: [id])
  userId      String
  createdAt   DateTime  @default(now())
  updatedAt   DateTime  @updatedAt
  startedAt   DateTime?
  endedAt     DateTime?
  superTaskId String?
  superTask   Task?     @relation(name: "Subtask", fields: [superTaskId], references: [id], onDelete: NoAction, onUpdate: NoAction)
  subtasks    Task[]    @relation(name: "Subtask")
  taskNum     Int?
  tags        Tag[]     @relation(name: "Tags")
  isArchived  Boolean   @default(false)

  @@index([userId])
  @@index([superTaskId])
}

Environment & setup

Prisma Version

prisma@4.14.1
lightbluepoppy commented 1 year ago

https://github.com/omar-dulaimi/prisma-zod-generator/issues/78#issuecomment-1467270064

Downgrading zod from 3.21.4 to 3.21.1 worked for me.