如何在 Prisma 中创建多对多连接 table 的一对多关系

How to create one to many relationship from many to many join table in Prisma

我正在创建一个锻炼应用程序,并且想模拟用户和锻炼程序之间的关系。一个用户可以多次创建程序。

这是我的 Prisma 模型:

model User {
  id                      Int         @id @default(autoincrement())
  createdAt               DateTime    @default(now())
  email                   String      @unique
  firstName               String      @db.VarChar(50)
  lastName                String      @db.VarChar(50)
  password                String      @db.VarChar(191)
  
  programs                ProgramEnrollment[]
  
}

model ProgramEnrollment {
  program                 Program     @relation(fields: [programId], references: [id])
  programId               Int // relation scalar field (used in the `@relation` attribute above)
  user                    User @relation(fields: [userId], references: [id])
  userId                  Int // relation scalar field (used in the `@relation` attribute            
  assignedAt              DateTime @default(now())

  @@id([programId, userId])
}

model Program {
  id                    Int                 @id @default(autoincrement())
  name                  String   
  users                 ProgramEnrollment[]
}

上面的效果很好,但现在我要做的是让用户记录他们的个人程序结果,所以我添加以下内容:

model ProgramEnrollment {
  program                 Program     @relation(fields: [programId], references: [id])
  programId               Int // relation scalar field (used in the `@relation` attribute above)
  user                    User @relation(fields: [userId], references: [id])
  userId                  Int // relation scalar field (used in the `@relation` attribute            
  assignedAt              DateTime @default(now())
  userProgram             UserProgram[] 

  @@id([programId, userId])
}

model UserProgram {
  id                        Int                 @id @default(autoincrement())
  name                      String

  userProgramEnrollment     ProgramEnrollment @relation(fields: [programEnrollmentId], references: [id])
  programEnrollmentId       Int // relation scalar field (used in the `@relation` attribute above)
} 

当我进行上述更改时,出现以下错误:验证错误:参数 references 必须仅引用相关模型 ProgramEnrollment 中的现有字段。相关模型中不存在以下字段:id

为什么它不允许我从多对多连接创建一对多关系 table?

As docs states 无法在关系字段上定义复合 ID (@@id)。

你可以使用 @@uniquedefine a compound unique constraint 来代替,像这样:@unique([programId, userId]),然后只使用常规的自动生成的 id ProgramEnrollment 然后你就可以在 UserProgram

的关系中使用它

我只需要稍微调整 UserProgram 模型以考虑 ProgramEnrollment 模型中的多字段 ID。

model ProgramEnrollment {
  program                 Program     @relation(fields: [programId], references: [id])
  programId               Int // relation scalar field (used in the `@relation` attribute above)
  user                    User        @relation(fields: [userId], references: [id])
  userId                  Int // relation scalar field (used in the `@relation` attribute            
  assignedAt              DateTime    @default(now())
  userProgram             UserProgram[] 

  @@id([programId, userId])
}

model UserProgram {
  id                           Int                 @id @default(autoincrement())
  name                         String
  userProgramEnrollment        ProgramEnrollment @relation(fields: [programEnrollment_programId,  programEnrollment_userId], references: [programId, userId])
  programEnrollment_programId  Int
  programEnrollment_userId     Int 
} 

由于 ProgramEnrollment 使用两个字段作为其 id,我们必须在 UserProgram 模型中引用它们。