Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
how to find post by using findUnique in prisma with post id and prodCode

I want to find only one post matching by post id and prodCode

below is my query code. It doesn't work. If i change findUnique to findFirst. it works.

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

prisma model

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())
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Since Post.id is unique, you don't need to filter by prodCode as well. You could just query the post record with the needed id and then check if the connected product has the right prodCode.

I would just do this:

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
  }

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!