You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Prisma Node.js多关联查询带Where条件报错求助

Prisma多关联查询Where条件类型不匹配问题解决

你遇到的类型错误是因为Prisma对关联字段的过滤需要使用嵌套查询操作符,而非直接传入字段条件,具体解决方案分两种情况:

情况1:User与UserNotificationsSetup是一对一关联

如果你的Prisma Schema中定义的是一对一关系(比如User的UserNotificationsSetup字段加了@unique或关联配置里设置了unique: true),需要用is操作符包裹条件:

where: {
  Account: {
    active: true,
  },
  User: {
    UserNotificationsSetup: {
      is: { aaa: true },
    },
    UserNotificationsFrequencySetup: {
      is: { daily: true },
    },
  },
},
distinct: ['accountId'],
select: {
  Account: {
    select: {
      networkId: true,
    },
  },
},

情况2:User与UserNotificationsSetup是一对多关联

如果是一个用户对应多条通知配置记录,必须用some/every/none这类操作符明确匹配逻辑(示例用some表示至少有一条匹配):

where: {
  Account: {
    active: true,
  },
  User: {
    UserNotificationsSetup: {
      some: { aaa: true },
    },
    UserNotificationsFrequencySetup: {
      some: { daily: true },
    },
  },
},
distinct: ['accountId'],
select: {
  Account: {
    select: {
      networkId: true,
    },
  },
},

原因说明

Prisma的TypeScript类型系统会根据Schema中的关系类型,限制关联字段的过滤方式:

  • 一对一关联:允许用is直接匹配对象
  • 一对多/多对多关联:必须通过some/every/none指定集合匹配规则,直接传字段对象会触发类型不匹配错误。

先确认你的Prisma Schema里的关系定义,再选择对应写法即可解决问题。

内容的提问来源于stack exchange,提问作者easdsa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 01:40:03