Why is the delete being call after updating manytomany relation [TypeORM]

Viewed 7

I have to two related table which have manyToMany relation. the first one is CurriculumCourseVch

@Entity()
export class CurriculumCourseVch extends BaseEntity<CurriculumCourseVch> {
    //other columns//
    @ManyToMany( () => CurriculumProgramVch )
    @JoinTable( {name: "curriculum_program_course_vch"} )
    curriculumPrograms: CurriculumProgramVch[]
}

And the second one is CurriculumProgramVch

@Entity()
export class CurriculumProgramVch extends BaseEntity<CurriculumProgramVch> {
    //other columns//
   @ManyToMany( () => CurriculumCourseVch )
   @JoinTable( {
      name: "curriculum_program_course_vch", // table name for the junction table of this relation
      inverseJoinColumn: { name: 'curriculum_course_vch_id' }
   } )
      curriculumCourses: CurriculumCourseVch[]
}

The problem I am facing is after I insert the relation the deleting always be called.

curriculumCourse.curriculumPrograms = curriculumProgramList
await conn.manager.save(curriculumCourse)

And these are the logs I've got. There are 2 start transection.

[2022-09-15T09:37:38.657Z] query: START TRANSACTION
[2022-09-15T09:37:38.660Z] query: INSERT INTO 
`curriculum_program_course_vch`(`curriculum_program_vch_id`, `curriculum_course_vch_id`) 
VALUES (?, ?) -- PARAMETERS: [1,11]
[2022-09-15T09:37:38.670Z] query: UPDATE `curriculum_course_vch` SET `active` = ?, `deleted` = 
?, `updated_date` = ?, `version` = `version` + 1 WHERE `id` IN (?) -- PARAMETERS: [1,0,"2022- 
09-15T09:37:38.658Z",11]
[2022-09-15T09:37:38.676Z] query: SELECT `CurriculumCourseVch`.`id` AS 
`CurriculumCourseVch_id`, `CurriculumCourseVch`.`version` AS `CurriculumCourseVch_version`, 
`CurriculumCourseVch`.`updated_date` AS `CurriculumCourseVch_updated_date` FROM 
`curriculum_course_vch` `CurriculumCourseVch` WHERE `CurriculumCourseVch`.`id` = ? -- 
PARAMETERS: [11]
[2022-09-15T09:37:38.678Z] query: COMMIT

After the first transaction commit I got another one

[2022-09-15T09:37:38.694Z] query: START TRANSACTION
[2022-09-15T09:37:38.697Z] query: INSERT INTO 
`curriculum_program_course_vch`(`curriculum_program_vch_id`, `curriculum_course_vch_id`) 
VALUES (?, ?) -- PARAMETERS: [1,11]
[2022-09-15T09:37:38.707Z] query: DELETE FROM `curriculum_program_course_vch` WHERE 
(`curriculum_course_vch_id` = ? AND `curriculum_program_vch_id` = ?) -- PARAMETERS: [11,"1"]
[2022-09-15T09:37:38.715Z] query: COMMIT

I don't understand why the deleting always be called. The problem is because I defined the relation wrong or it's because I used conn.manager for saving? Has anyone run into this problem? Thanks for your attention. I'm looking forward to your reply.

0 Answers
Related