Skip to content

Remove orphaned entities from a relation? #6382

@RobbieGM

Description

@RobbieGM

Issue type:

[x] question
[ ] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[x] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[ ] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

I have a one-to-many relation 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. I saw a PR for this feature but it was rejected. Is there a way to do this, or any plans to support it in the future?

I would like to be able to do something like this:

@Entity()
@TableInheritance({ column: { name: 'type', type: 'text' } })
export default class Geolocation {
  @PrimaryGeneratedColumn()
  id!: number;

  @IsGeoJSONPoint
  @Column('geography')
  point!: Point;
}

@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[];
}

// somewhere in the API...

const business = new Business();
business.geolocations = [{ type: 'Point', coordinates: [3, 5] }, { type: 'Point', coordinates: [8, 2] }];
await manager.save(business);
business.geolocations = [{ type: 'Point', coordinates: [6, -3] }]; // I want all the old geolocation rows to be deleted here
await manager.save(business);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions