How to model a tournament in Prisma

grabury

I want to model a tournament in Prisma. I have this:

model Tournament {
  id      Int     @id @default(autoincrement())
  meets   Meet[]
}

model Meet {
  id      Int     @id @default(autoincrement())
  name    String
  matches Match[]
}

model Match {
  id            Int     @id @default(autoincrement())
  player1Id     Int
  player2Id     Int
  meet          Meet @relation(fields: [meetId], references: [id])
  meetId        Int
}

model Player {
  id    Int     @id @default(autoincrement())
  name  String
}

model Result {
  id        Int     @id @default(autoincrement())
  matchId    Int
  playerId  Int
}

I feel the Match model might need to link player1 and player2 (with a relation) to the Player model. I'm also not sure how the result should be modelled.

Any advice will be appreciated.

luisbar

You can do it as follow:

datasource mysql {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

generator erd {
  provider = "prisma-erd-generator"
  output = "./entity-relationship-diagram.svg"
}

model Tournament {
  id      Int     @id @default(autoincrement())
  name String
  meets   Meet[] @relation()
}

model Meet {
  id      Int     @id @default(autoincrement())
  name    String
  tournament Tournament @relation(fields: [tournamentId], references: [id])
  tournamentId Int
  matches Match[] @relation()
}

model Match {
  id            Int     @id @default(autoincrement())
  name          String
  meeet        Meet    @relation(fields: [meetId], references: [id])
  meetId       Int
  players      Player[] @relation("matchPlayers", fields: [playerId], references: [id])
  playerId     Int
  winner       Player?  @relation("matchWinner", fields: [winnerId], references: [id])
  winnerId     Int?
  score        Score[] @relation()
}

model Player {
  id    Int     @id @default(autoincrement())
  name  String
  matchesPlayers Match[] @relation("matchPlayers")
  matchesWinner Match[] @relation("matchWinner")
  score Score[] @relation()
}

model Score {
  id    Int     @id @default(autoincrement())
  score Int
  match Match    @relation(fields: [matchId], references: [id])
  matchId Int
  player Player  @relation(fields: [playerId], references: [id])
  playerId Int
}

And it will generate the following database

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related