TypeORM duplicate Key value for ManyToMany relation

Viewed 198

Context
I use TypeORM and NestJS.
I have two entities :

  • workout
  • exercise

A workout can I have several exercises and an exercise can have several workouts. To implement this I use a ManyToMany relation.

Workout entity

@Entity()
export class Workout {
  @PrimaryGeneratedColumn()
  id: number;

  @JoinTable()
  @ManyToMany(() => Exercise, (exercise) => exercise.workouts)
  exercises: Exercise[];

}

Exercise entity

@Entity()
export class Exercise {
  @PrimaryGeneratedColumn()
  id: number;

  @ManyToMany(() => Workout, (workout) => workout.exercises)
  workouts: Workout[];
}

Everything works well but I got an error message duplicate key value violates unique constraint when I try to create a workout with two same exercises.

Question : how can I have duplicate entities in a ManyToMany relation ?

0 Answers
Related