'how can i update or sync a n:m relation with Prisma?

I have a problem with nest.js and prisma (V3). I have a table training and a n:m table player_training. The table player_training looks like this:

training_id, player_id, type, team_id.

Dataset example:

1, 2, 'e', 1
1, 4, 'a', 1

I can create records. This works fine. But I need to change and delete records of a training (patch request). It seems that prisma has no sync function for update, create and delete the records? How can I first delete the records of a training and then create new records again in one go?

I saw an example in the Prisma documentary. But I can't implement it for my example.

The approach would be:

const test = await prisma.training.update({
  where: {
    id: 1,
  },
  data: {
    player_training: {
      createMany: {
        data: [{ player_id: 2 }...],
      },
    },
  },
})

This does not work. Wrong structure and approach?

Update: Parts of the scheme:

model training {
  id               Int                @id(map: "PK_c436c96be3adf1aa439ef471427") @unique(map: "training_id") @default(autoincrement())
  status           String             @db.Char(1)
  trainingsdat     DateTime           @db.Date
  created_at       DateTime           @default(now()) @db.Timestamptz(6)
  updated_at       DateTime           @default(now()) @db.Timestamptz(6)
  team_id          Int
  team       team         @relation(fields: [team_id], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "FK_fffd85ffa1a2587d0e9fa031ad9")
  player_training player_training[]

  @@unique([trainingsdat, team_id], map: "team_trainingsdat_teamId")
}


model player_training {
  player_id    Int
  training_id   Int
  team_id Int
  status        String     @db.VarChar(1)
  team    team @relation(fields: [team_id], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "FK_4e78ce1ea0f73af090f32107c2c")
  player       player    @relation(fields: [player_id], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "FK_0ec307abcaeb1911e30eb0d6727")
  training      training   @relation(fields: [training_id], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "FK_e116f7ef2d5abb20967360c2221")

  @@unique([player_id, training_id, team_id], map: "team_training_player_id_training_id_player_id")
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source