如果存在可选关系,我该如何过滤?

How do I filter if an optional relationship exists?

我有以下架构,其中 QuizUser 上的可选关系。

model User {
  email String @id @unique
  quiz  Quiz?
}

model Quiz {
  id        String @id @default(uuid())
  user      User   @relation(fields: [userEmail], references: [email], onDelete: Cascade)
  userEmail String
}

我想找到所有确实有测验关系的用户。

起初我天真地尝试了以下方法:

const users = await p.user.findMany({
  where: { quiz: true },
})

然后我想我找到了解决办法in their docs.

const users = await p.user.findMany({
  where: { quiz: { some: {} } },
})

但这给出了错误:

Type '{ some: {}; }' is not assignable to type '(Without<QuizRelationFilter, QuizWhereInput> & QuizWhereInput) | (Without<QuizWhereInput, QuizRelationFilter> & QuizRelationFilter) | null | undefined'.

Object literal may only specify known properties, and 'some' does not exist in type '(Without<QuizRelationFilter, QuizWhereInput> & QuizWhereInput) | (Without<QuizWhereInput, QuizRelationFilter> & QuizRelationFilter)'.

9 where: { quiz: { some: {} } },

您在文档中找到的解决方案适用于一对多关系。可以像这样过滤一对一关系:

const users = await p.user.findMany({
  where: { quiz: { isNot: null } },
});