两个字段可以引用另一个模型上的同一个字段吗

Can two fields reference the same field on another model

我的 prisma 模式中有两个模型,用于表示一种游戏时间表,其中 2 个“玩家”可以在“对战”中相互对战:

model Matchup {
  id             Int @id @default(autoincrement())
  homePlayer     Player @relation("Home Player", fields: [homePlayerId], references: [id])
  homePlayerId   Int
  awayPlayer     Player @relation("Away Player", fields: [awayPlayerId], references: [id])
  awayPlayerId   Int
}

model Player {
  id            Int @id @default(autoincrement())
  homeMatchUps  Matchup[] @relation("Home Player")
  awayMatchUps  Matchup[] @relation("Away Player")
}

不过,我不一定需要区分“主场”球员和“客场”球员。我宁愿 Player 模型对他们关联的任何对战有一个单一的引用,而不是必须在两个字段之间拆分它。

解决这个问题的正确方法是什么?

不可能从 Player 端对 Matchup 进行单个引用,您需要有两个。所以你要求的东西真的做不到。

原因是,homePlayerawayPlayer 代表两个独立的 one-to-many 关系,您需要在多边有一个匹配字段(在本例中 Player ) 对于每一个 one-to-many 关系。