Prisma Client关联表存在性查询报错及需求实现问询
解决Prisma关联查询中数组关系的筛选错误
你的问题出在对Prisma数组类型关联的筛选语法使用错误:casino_p_casinos的bonuses是一对多的数组关联,直接在where的bonuses下写nodeposit字段不符合Prisma的类型规则,必须使用集合谓词来筛选数组关系。
修改后的查询代码
const data = await prisma.casino_p_casinos.findMany({ where: { approved: 1, rogue: 0, // 使用some谓词:只要存在至少一个bonus满足nodeposit>0即可 bonuses: { some: { nodeposit: { gt : 0 }, } } }, select: { id: true, clean_name: true, casino: true, button: true, bonuses: true, }, take: 14, });
关键修改说明
- 原代码中直接访问
bonuses.nodeposit的写法错误,因为bonuses是数组,无法直接定位到单个元素的字段。 - 使用
some谓词可以实现需求中的“关联的bonus中存在nodeposit>0”逻辑:它会检查当前casino的所有bonus记录,只要有一条符合nodeposit > 0的条件,该casino就会被选中。 - 查询返回的
bonuses仍然是该casino的全部bonus记录,符合你“获取该casino的全部bonus”的需求。
内容的提问来源于stack exchange,提问作者Radium Chris
相关产品推荐
相关产品推荐

