NestJS+Prisma ORM:筛选未关联observation的活跃SIM卡
解决方案
要筛选出状态为Active(active: true)且未被observation表使用的SIM卡,核心是在查询条件中排除已关联observation的记录——也就是sim_card表中observationId为null的条目。
修改后的findActive方法如下:
findActive(): Promise<CreateSimCardDto[]> { return this.prismaService.sim_card .findMany({ where: { active: true, // 筛选未被observation关联的SIM卡 observationId: null } }) .catch((error) => { throw new HttpException(error.message, HttpStatus.BAD_REQUEST); }); }
关键说明
- 原代码仅过滤了
active: true的SIM卡,但未排除已被observation占用的记录。在一对一关系下,sim_card的observationId不为空时,说明该卡已分配给某个observation。 - 移除了不必要的
include配置,因为我们只需获取SIM卡自身数据,无需关联的observation信息,能提升查询效率。
内容的提问来源于stack exchange,提问作者Oleksandr Yer
相关产品推荐
相关产品推荐

