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

如何在Prisma查询的WHERE子句中实现start_date或条件过滤

Prisma查询实现多条件组合过滤

要实现类似SQL中WHERE (id_1, id_2, id_3) IN ([array]) AND (start_date < today OR start_date IS NULL)的查询效果,正确的Prisma写法需要将start_date的OR条件与原有的ID组合条件通过外层AND逻辑绑定,确保两个条件组同时生效。

正确代码示例

where: {
  AND: [
    {
      OR: ids.map((id) => ({
        id_1: id[1],
        id_2: id[2],
        id_3: id[3],
      })),
    },
    {
      OR: [
        { start_date: { lt: today } },
        { start_date: null }, // 等价于equals: null,Prisma支持直接写null简化写法
      ],
    },
  ],
},

之前方案无效的原因

  • start_date: { OR: [{ lt: today }, { equals: null }] }:Prisma不支持在字段层级直接使用OR语法,逻辑操作符需作为顶层或嵌套在逻辑块中。
  • start_date: [{ lt: today }, { equals: null }]:不符合Prisma查询语法规范,字段过滤条件不能直接传入数组。
  • AND: [{ start_date: { lt: today } }, { start_date: { equals: null } }]:单独添加该AND会覆盖原有ID组合的OR条件,导致逻辑变为**(start_date < today) AND (start_date IS NULL)**,两个条件互斥,无法返回有效数据。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 23:35:18