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

如何在Prisma中查询归属当前用户的指定Update记录

问题:根据Update ID查询记录并验证归属当前登录用户

需求说明

现有Prisma数据模型关系:用户(User)拥有多个产品(Product),产品拥有多个更新记录(Update)。Update表仅关联Product,无直接关联User的外键。需要实现:根据Update的ID查询该记录,同时确保它属于当前登录用户。

错误的尝试代码

尝试使用以下查询,但返回结果包含当前用户的所有产品(即使产品不包含目标Update),不符合预期:

const product = await prisma.product.findMany({
  where: { belongsToId: req.user.id },
  include: {
    updates: {
      where: { id: req.params.id },
    },
  },
});

Prisma数据模型

model User {
  id        String    @id @default(uuid()) @db.Uuid
  createdAt DateTime  @default(now())
  username  String    @unique @db.VarChar(255)
  password  String    @db.VarChar(255)
  products  Product[]
}
model Product {
  id          String   @id @default(uuid()) @db.Uuid
  createdAt   DateTime @default(now())
  name        String   @db.VarChar(255)
  belongsToId String   @db.Uuid
  belongsTo   User     @relation(fields: [belongsToId], references: [id])
  updates     Update[]
  @@unique([id, belongsToId])
}
model Update {
  id        String   @id @default(uuid()) @db.Uuid
  createdAt DateTime @default(now())
  updatedAt DateTime

  title  String        @db.VarChar(255)
  body   String
  status UPDATE_STATUS @default(IN_PROGRESS)

  version String?
  asset   String?

  productId    String        @db.Uuid
  product      Product       @relation(fields: [productId], references: [id])
  updatePoints UpdatePoint[]
}

错误返回结果示例

{
  "data": [
    {
      "id": "bdd8d760-de94-4693-abab-b4685b52ac93",
      "createdAt": "2022-11-12T18:04:09.517Z",
      "name": "Note Stuff app",
      "belongsToId": "5754b464-9446-44de-9135-0beb6cac83f8",
      "updates": []
    },
    {
      "id": "f9a7b8cf-b0fc-443f-a9fc-b1ea8ff42799",
      "createdAt": "2022-11-13T05:23:20.497Z",
      "name": "Sledge Hammer",
      "belongsToId": "5754b464-9446-44de-9135-0beb6cac83f8",
      "updates": [
        {
          "id": "4f781638-0d39-4e65-816d-0fd816721c26",
          "createdAt": "2022-11-13T05:25:21.614Z",
          "updatedAt": "1970-01-01T00:00:00.000Z",
          "title": "Adding metal",
          "body": "Sledge hammer is now more heavy",
          "status": "IN_PROGRESS",
          "version": null,
          "asset": null,
          "productId": "f9a7b8cf-b0fc-443f-a9fc-b1ea8ff42799"
        }
      ]
    }
  ]
}

正确解决方案

方法1:直接查询Update并验证归属(推荐)

直接通过update.findUnique查询目标记录,同时通过嵌套条件验证该Update所属的Product归属于当前用户。这种方式更高效,只会返回符合条件的Update(或null),不会返回无关产品:

const targetUpdate = await prisma.update.findUnique({
  where: {
    id: req.params.id,
    // 嵌套验证所属产品的用户ID
    product: {
      belongsToId: req.user.id
    }
  },
  // 可选:如果需要关联产品信息,可添加include
  include: {
    product: true
  }
});

方法2:查询Product并过滤包含目标Update的记录

如果需要同时获取产品信息,也可以通过product.findFirst查询,确保产品属于当前用户且包含目标Update:

const productWithTargetUpdate = await prisma.product.findFirst({
  where: {
    belongsToId: req.user.id,
    updates: {
      some: {
        id: req.params.id
      }
    }
  },
  include: {
    updates: {
      where: { id: req.params.id }
    }
  }
});

原代码问题分析

原代码使用product.findMany时,where条件仅过滤用户的产品,include中的updates.where只是过滤每个产品下的更新记录,并不会排除没有目标Update的产品,因此所有用户的产品都会被返回,只是无目标Update的产品的updates数组为空。

内容的提问来源于stack exchange,提问作者Muhammad Hassan Javed

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 09:15:34