簡體   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