【问题标题】:Prisma return wrong typePrisma 返回错误类型
【发布时间】:2022-12-08 02:53:35
【问题描述】:

我有这个 schema.prisma 和这样的代码:

const args = Prisma.validator<Prisma.SelectorFindFirstOrThrowArgs>()({
  where: {
    kind: "Role",
  },
  select: {
    clarification: {
      select: {
        question: true,
        answers: {
          select: {
            id: true,
            title: true,
            role: {
              select: {
                id: true,
                title: true,
              },
            },
            clarification: {
              select: {
                question: true,
                answers: true,
              },
            },
          },
        },
      }
    },
  },
})

const selector = prisma.selector.findFirstOrThrow(
  args,
)

现在 selector 的类型是

{
  id: string;
  title: string;
  clarification: {
    question: string;
    answers: Option[];
  };
  role: {
    id: string;
    title: string;
  };
}

但我希望 selector 的类型应该是

{
  id: string;
  title: string;
  clarification: {
    question: string;
    answers: Option[];
  } | null;
  role: {
    id: string;
    title: string;
  } | null;
}

为什么Prisma解析这个类型错误?

Repository 具有最小的可重现示例

【问题讨论】:

    标签: typescript prisma


    【解决方案1】:

    clairfication 在你的 Selector 模型上没有标记为可为空,因此它总是期望出现在数据库级别。您需要在 Prisma 模式中将其标记为可为空,以便 Prisma 将其输出视为 TypeScript 中的 Clarification | null

    model Selector {
      id              String        @id @default(uuid())
      createdAt       DateTime      @default(now())
      updatedAt       DateTime      @updatedAt
      kind            SelectorKind  @unique
      clarification   Clarification? @relation(fields: [clarificationId], references: [id])
      clarificationId String?
      @@map("selectors")
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-04
      • 2019-12-12
      相关资源
      最近更新 更多