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

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 02:17:35