Stian Bakken
02/28/2022, 2:40 PMmodel 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 🙂nikolasburk
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.nikolasburk
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?nikolasburk
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")
}
Stian Bakken
02/28/2022, 2:57 PMteams Team[]
relation and handle the logic in code?Stian Bakken
02/28/2022, 2:58 PMtype TeamType
which could be an enum with Home, Away or somethingStian Bakken
02/28/2022, 3:01 PMmodel 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")
}
Stian Bakken
02/28/2022, 3:04 PM