Hello everyone and merry christmas! Just started l...
# orm-help
v
Hello everyone and merry christmas! Just started looking at prisma and it seems cool! I have a question about a many to many relation.
Copy code
model User {
  id           String               @id @default(uuid())
  Squads       UserSquadMap[] // I would like this to be an implicit maping of the actual mapped Squad. => Squad[]
  OwnedSquads  Squad[]
}

model Squad {
  id          String
  owner_id    String
  Owner       User           @relation(fields: [owner_id], references: [id], onDelete: NoAction, onUpdate: NoAction)
  Members     UserSquadMap[] // And this obviously =>  Users[]
}

// The Join Table
model UserSquadMap {
  userId  String
  squadId String
  User    User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  Squad   Squad    @relation(fields: [squadId], references: [id], onDelete: Cascade)

  @@id([userId, squadId])
  @@index([squadId])
  @@index([userId])
}
I want to directly reference the JoinTable relation entity so that I don't have nested
includes {}
and nested responses with irrelevant data. Right now I'm just transforming the object in the actual response, but surely there's a better way to directly reference it somehow? Is this possible? (I have been looking at
@@map
but no no avail, yet)
m
If that multi field @@id and indexes are not required, you can use Implicit Many to Many relation. https://www.prisma.io/docs/concepts/components/prisma-schema/relations/many-to-many-relations#implicit-many-to-many-relations Otherwise you would need to transform that object with map, yes. https://www.prisma.io/docs/support/help-articles/working-with-many-to-many-relations
v
Allright thanks!
Yeah, implicit did the job/what I asked for 🙂 , Now when I think about it I suppose the nested
includes
kinda make sense. And this feels more like the API's task, not the ORMs, Especially since I will probably need addtional data (such as createdAt) on the map row itselves which can't be done Implicitly
Im sure it has its use-cases tho!
m
Yes if you need some additional fields, indexes or PK on the relation table, then you will need to use Explicit Many to Many. Also I think there are some problems on hosted DB like Digital Ocean since there is no PK (not sure tho): https://github.com/prisma/docs/issues/958
v
Thanks for the help & heads up!