繁体   English   中英

如何通过在带有帖子 ID 和产品代码的 Prisma 中使用 findUnique 来查找帖子

[英]how to find post by using findUnique in prisma with post id and prodCode

我只想找到一个与帖子 ID 和 prodCode 匹配的帖子

下面是我的查询代码。 它不起作用。 如果我将 findUnique 更改为 findFirst。 有用。

const post = await client.post.findUnique({
          where: {
            AND: [
              { id: postId },
              {
                product: {
                  prodCode,
                },
              },
            ],
          },
        });

棱镜模型

model Product {
  id       Int       @id @default(autoincrement())
  prodName String
  prodCode String    @unique
  posts    Post[]
  holdings Holding[]
  proposes Propose[]

}

model Post {
  id        Int      @id @default(autoincrement())
  user      User     @relation(fields: [userId], references: [id])
  userId    Int
  product   Product  @relation(fields: [productId], references: [id])
  productId Int
  title     String
  content   String
  createdAt DateTime @default(now())
}

由于Post.id是唯一的,因此您也不需要prodCode进行过滤。 您可以使用所需的id查询post记录,然后检查连接的product是否具有正确的prodCode

我只会这样做:

const post = await prisma.post.findUnique({
    where: {
      id: postId
    },
    include: {
      product: true
    }
  });

  if (post.product.prodCode === prodCode) {
    // No result for desired query
  } else {
    // "post" variable contains result of desired query
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM