sqlprismaprisma2

How to search this Query using prisma?


I'm making database of travel destination which people have been to by many-to-many relationship.

model Person {
  id String  @id @default(cuid())
  name String
  personCountry PersonCountry[]
}

model Country {
  id String  @id @default(cuid())
  name String
  personCountry PersonCountry[]
}

model PersonCountry{
  id String  @id @default(cuid())
  person Person @relation(fields: [personId], references: [id], onDelete: onCascade)
  personId String
  country Country @relation(fields: [countryId], references: [id], onDelete: onCascade)
  countryId String
}

for example,

const persons = [{ id: "1", name: "John" }, { id: "2", name: "Jack" }]
const countries = [{ id: "1", name: "USA" }, { id: "2", name: "China" }]

if John has been to USA and China,

personCountry: [{ personId: "1", countryId: "1" }, { personId: "1", countryId: "2" }]

If you want to search people who have been to both of USA and China, what query should be made?

What SQL should be appropriate if I code raw SQL?


Solution

  • OK, use AND

    prisma.person.findFirst({
    where: {
      AND: [{ ... }, { ... }]
    },
    

    });