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

Prisma查询含非空Menu的SubCategory报错求助

问题解决

错误原因

你写的id: { not: null }不符合Prisma的语法规范,not操作符不能直接传入null;同时当前查询没有筛选本身关联了Menu的SubCategory,只是在关联查询时过滤Menu,会导致结果里仍包含无Menu的SubCategory(仅其menu字段为空数组)。

修正后的查询代码

const topCategories = await this.prisma.subCategory.findMany({
  // 筛选出至少关联一个Menu的SubCategory
  where: {
    menu: {
      some: {}
    }
  },
  include: {
    menu: true // 直接关联查询所有对应Menu,无需额外过滤(id为主键必然非空)
  },
  orderBy: {
    id: 'desc',
  },
  take: 50,
});

关键说明

  • where: { menu: { some: {} } }是核心:确保只返回至少关联一个Menu的SubCategory,精准匹配你"包含非空Menu的SubCategory"的需求。
  • 移除include里的where条件:Menu的id是自增主键,数据库层面不可能为null,无需冗余过滤。
  • 若需过滤Menu的其他非空字段(比如name非空),可使用isNot语法,示例:
    include: {
      menu: {
        where: {
          name: { isNot: null }
        }
      }
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 20:04:52