question on Cascading deletes with SQLite. I have...
# orm-help
a
question on Cascading deletes with SQLite. I have a few models and I if I want to delete a Player, I want all other associated data to be deleted as well
Copy code
model Player {
  id        String        @id
  username  String?
  createdAt DateTime      @default(now())
  updatedAt DateTime      @default(now())
  losses    Match[]       @relation("Losses")
  resigned  Match[]       @relation("Resigned")
  wins      Match[]       @relation("Wins")
  moves     Move[]
  matches   PlayerMatch[]
  rolls     Roll[]
  lastLogin DateTime?
}
Copy code
model Match {
  id         String        @id
  status     String        @default("active")
  winnerId   String?
  loserId    String?
  resignedId String?
  createdAt  DateTime      @default(now())
  updatedAt  DateTime      @default(now())
  endedAt    DateTime?
  loser      Player?       @relation("Losses", fields: [loserId], references: [id])
  resigned   Player?       @relation("Resigned", fields: [resignedId], references: [id])
  winner     Player?       @relation("Wins", fields: [winnerId], references: [id])
  moves      Move[]
  rolls      Roll[]
  players    PlayerMatch[]
}
Copy code
model Move {
  id        Int      @id @default(autoincrement())
  matchId   String
  playerId  String
  createdAt DateTime @default(now())
  updatedAt DateTime @default(now())
  type      String?
  selected  String   @default("")
  match     Match    @relation(fields: [matchId], references: [id])
  player    Player   @relation(fields: [playerId], references: [id])
}

model Roll {
  id        Int      @id @default(autoincrement())
  matchId   String
  playerId  String
  createdAt DateTime @default(now())
  updatedAt DateTime @default(now())
  die1      Int
  die2      Int
  match     Match    @relation(fields: [matchId], references: [id])
  player    Player   @relation(fields: [playerId], references: [id])
}

model PlayerMatch {
  playerId  String
  matchId   String
  createdAt DateTime @default(now())
  updatedAt DateTime @default(now())
  match     Match    @relation(fields: [matchId], references: [id], onDelete: Cascade)
  player    Player   @relation(fields: [playerId], references: [id], onDelete: Cascade)

  @@id([playerId, matchId])
}
Copy code
delete player error PrismaClientKnownRequestError: Foreign key constraint failed on the field: `foreign key`
n
Hey 👋 On deleting record from which table are you getting the above error?
👋 1
a
Player
👋 any thoughts @Nurul. I always appreciate your assistance
👍 1
n
Let me try to reproduce it locally
a
thank you. I have cascade on delete for the Join Table
PlayerMatch
but wondering how I would do that if I'm removing a Player
👀 1