Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

242
Visualizações
MongoDB & Prisma: unilateral M-to-N relation

Is there a way to make an unilateral m-to-n relation or must both collections have each other's ids? I'm trying to do something like this:

model Country {
  id              String    @id @default(auto()) @map("_id") @db.ObjectId
  name            String    @unique
  users           User[]
}

model User {
  id              String    @id @default(auto()) @map("_id") @db.ObjectId
  userName        String    @unique
  countryIds      String[]  @db.ObjectId
  countries       Country[] @relation(fields: [countryIds], references: [id])
  // ....
}

But prisma is making me add another field to Country to store the users ids... Like this:

model Country {
  id              String    @id @default(auto()) @map("_id") @db.ObjectId
  name            String    @unique
  userIds         String[]  @db.ObjectId
  users           Player[]  @relation(fields: [userIds], references: [id])
}

I don't need that data and it's not logically needed. Is there any way to bypass that? Any workaround?

EDIT #1:

After some testing I've found that if I leave that userIds field, even though it does not exist in any document, the user's countries could still be correctly queried. The only drawback(not really) is that I can't query the users from the countries. But I guess it makes sense since if I actually needed to query them, adding the userIds relation to the country would also make sense. The actual problem is the error that doesn't let me build/run the code without userIds even though it works fine without it.

I'm gonna leave this unanswered in case there's something I'm missing and someone wants to help out but I'll create an answer with the content of this edit if no one comes forward.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda