How to get records with conditions from multiple t...
# orm-help
m
How to get records with conditions from multiple tables. Schema: model User { id Int @id @default(autoincrement()) email String @unique name String? posts Post[] } model Post { id Int @id @default(autoincrement()) title String published Boolean @default(false) author User @relation(fields: [authorId], references: [id]) authorId Int } Query: To get users post where user name will be "john" OR post title will be "First Post"?
1
n
Hello @mazhar iqbal 👋 Here’s an example on how you can achieve this:
Copy code
const response = await prisma.user.findMany({
    where: {
      OR: [
        {
          name: 'John',
        },
        {
          posts: {
            some: {
              title: 'First Post',
            },
          },
        },
      ],
    },
    include: {
      posts: true,
    },
  });
Sample Response:
Copy code
[
   {
      "id":1,
      "email":"<mailto:test@prisma.io|test@prisma.io>",
      "name":"Test",
      "posts":[
         {
            "id":1,
            "title":"First Post",
            "published":false,
            "authorId":1
         }
      ]
   },
   {
      "id":2,
      "email":"<mailto:john@prisma.io|john@prisma.io>",
      "name":"John",
      "posts":[]
   }
]
m
Thank you
🙏 1