Hey everyone, I have a silly issue that I can't se...
# orm-help
s
Hey everyone, I have a silly issue that I can't seem to figure out in regards to my Schema. I'm trying to model a Match, where two teams go up against eachother. A match can only ever have two teams, so I thought something like this might make sense, but it gives me an error
Copy code
model Match {
  id         String    @id @default(uuid())

  teamA Team @relation(name: "teamA", fields: [teamAId], references: [id])
  teamB Team @relation(name: "teamB", fields: [teamBId], references: [id])

  teamAId String @unique
  teamBId String @unique
}

model Team {
  id String @id @default(uuid())

  match   Match   @relation(fields: [matchId], references: [id])
  matchId String
}
If anyone has any better ways to model this or any ideas about how to solve it, I'd really appreciate it 🙂
n
Hey Stian đź‘‹ your issue is that team currently only has two relation fields from
Match
to
Team
but only one “back-relation” field from
Team
to
Match
. In order to make your schema work, the number of relation fields have to match on each side. You can read more about this in the docs here.
👍 1
Also, two notes I noticed about the schema: • you have to decide which side of the relation should hold the foreign key, my thinking it’s probably better to have it on
Match
(you already defined those as
teamAId
and
teamBId
), so you can remove the
fields
and
references
from the
@relation
attribute on
Match
(you do need the
@relation
attribute though to disambiguate the relation, see the docs page I’ve shared in the last message) • right now you’re defining a 1-1 relation, which means one team can only ever have exactly one match. is that intentional or should this rather be a 1-n relation where one team can have many matches?
I haven’t tested it but I suspect your schema needs to look a bit more like this:
Copy code
model Match {
  id String @id @default(uuid())

  teamA Team @relation(name: "teamA", fields: [teamAId], references: [id])
  teamB Team @relation(name: "teamB", fields: [teamBId], references: [id])

  teamAId String @unique
  teamBId String @unique
}

model Team {
  id String @id @default(uuid())

  matchesAsTeamA Match[] @relation("teamA")
  matchesAsTeamB Match[] @relation("teamB")
}
s
Thanks for the quick response! I do think my schema can probably be modelled differently. These are "pickup" matches, so teams will be different everytime. So a team will only exist in one match. Maybe it would be better to just make a
teams Team[]
relation and handle the logic in code?
Then I could have a
type TeamType
which could be an enum with Home, Away or something
Or I guess something like this could also work and be "safe", since a Match would require a home and an away team to be saved.
Copy code
model Match {
  id         String    @id @default(uuid())
  startedAt  DateTime?
  finishedAt DateTime?

  homeTeam Team @relation(name: "homeTeam", fields: [homeTeamId], references: [id])
  awayTeam Team @relation(name: "awayTeam", fields: [awayTeamId], references: [id])

  homeTeamId String @unique
  awayTeamId String @unique
}

model Team {
  id String @id @default(uuid())

  matchAsHomeTeam Match? @relation("homeTeam")
  matchAsAwayTeam Match? @relation("awayTeam")
}
Either way, thanks so much for your help! I've written multiple apps with different ORM's like sequelize/TypeORM/Mikroorm, but I tried Prisma out for the last application I built and it's just amazing. Never did I feel like I was fighting the framework, I was just constantly building out my features and focusing on the application. I love it!
prisma rainbow 2
đź’š 2