TypeORM tries to reinsert many-to-many soft-releted relation

80 Views Asked by At

I have these related entities:

#Entity('vendors')
class Vendor {
  ...
  @DeleteDateColumn({ type: 'timestamptz' })
  deletedAt: Date;
}

@Entity('components')
class Component {
  ...
  @ManyToMany(() => Vendor, { eager: true })
  @JoinTable({
    name: 'componentsVendors',
    joinColumn: {
      name: 'componentId',
      referencedColumnName: 'id',
    },
    inverseJoinColumn: {
      name: 'vendorId',
      referencedColumnName: 'id',
    },
  })
  vendors: Vendor[];
}

When I try to do a repository.save to an already saved component (just updating another field) with two vendors, which the second one is soft-deleted, TypeORM tries to re-insert this second vendor into the join table.

In the logs I can see a select from the join table, where vendors.deletedAt is NULL and, right after, a insert into the join table with this soft-deleted vendor, which causes a duplicate key value violates unique constraint.

How can I do TypeORM to ignore delete status of many-to-many relations?

0

There are 0 best solutions below