I have a one-to-many relation in TypeORM and I would like to delete rows from the many side of the relationship rather than unlinking them by setting their foreign key to null (the default behavior), leaving them orphaned. How can this be done? I saw a PR for this feature but it was rejected. Is there a workaround or some other way to do this? Here's a simplified version of my code to give some context.
@Entity()
@TableInheritance({ column: { name: 'type', type: 'text' } })
export default class Geolocation {
@PrimaryGeneratedColumn()
id!: number;
@IsGeoJSONPoint
@Column('geography')
point!: Point;
}
@ChildEntity('offer')
export default class OfferGeolocation extends Geolocation {
@ManyToOne(type => Offer, offer => offer.geolocations, { onDelete: 'CASCADE' })
offer!: Offer;
}
@ChildEntity('business')
export default class BusinessGeolocation extends Geolocation {
@ManyToOne(type => Business, business => business.geolocations, { onDelete: 'CASCADE' })
business!: Business;
}
@Entity()
export default class Business {
@PrimaryGeneratedColumn()
id!: number;
// I would like to remove orphaned business geolocations
@OneToMany(type => BusinessGeolocation, businessGeolocation => businessGeolocation.business, { cascade: true })
geolocations!: BusinessGeolocation[];
}
@Entity()
export default class Offer {
@PrimaryGeneratedColumn()
id!: number;
// I would like to remove orphaned offer geolocations as well
@OneToMany(type => OfferGeolocation, offerGeolocation => offerGeolocation.offer, { cascade: true })
geolocations!: OfferGeolocation[];
}