How to translate this query to prisma? ```SELECT ...
# orm-help
v
How to translate this query to prisma?
Copy code
SELECT id, name from posts where id IN (select post_id from comments where user_id='123');
o
One approach would be to do the inner select first. Await the results. And then loop through them and do The outer select on each entry you got back.
Assuming prisma wear does not have an in operation.
v
@Omar Hamdan That's really a costly approach :/
r
@Vignesh 👋 Assuming this is your schema:
Copy code
model User {
  id       Int       @id @default(autoincrement())
  name     String
  posts    Post[]
  comments Comment[]
}

model Post {
  id       Int       @id @default(autoincrement())
  title    String
  comments Comment[]
  user     User?     @relation(fields: [userId], references: [id])
  userId   Int?
}

model Comment {
  id      Int    @id @default(autoincrement())
  comment String
  author  User?  @relation(fields: [userId], references: [id])
  post    Post?  @relation(fields: [postId], references: [id])
  postId  Int?
  userId  Int?
}
It could be done as follows:
Copy code
await prisma.comment.findMany({
    where: { userId: 123 },
    include: { post: true },
  })
v
@Ryan Thanks for the reply.. But It'll return comments data which i don't want.. Is there a way to unselect all fields of comment instead of explicitly mentioning false to each field?
r
You can use
select
then instead of
include
and specify the required fields.