Skip to content

Delete Referenced Record instead of setting fk reference to null on OneToMany save #1286

@crowebird

Description

@crowebird

Setup:


@Entity()
export Class EntityA extends BaseEntity {
  @PrimaryGeneratedColumn()
  EntityAId: number;

  @Column()
  SomeCol: string;

  @OneToMany(type => EntityB, entity => entity.EntityA, {
    cascadeInsert: true
  })
  EntityBs: EntityB[];
}

@Entity()
export Class EntityB extends BaseEntity {
  @PrimaryGeneratedColumn()
  EntityAId: number;

  @ManyToOne(type => EntityA, (entity: EntityA) => entity.EntityBs)
  @JoinColumn({ name: 'fkEntityA' })
  EntityA: EntityA;
}

...
let entity = await EntityA.findOne({
  SomeCol: 'SomeVal'
});

entity.EntityBs = [
  EntityB.create()
];

entity.save();

Say I already have an EntityA in the database with some EntityBs that reference it.

If I query entityA and then change entity.EntityBs to some new array of EntityBs (and maybe update some existing), the ones that are no longer in the array I want to DELETE from the database.

Right now they only get their fkEntityA reference set to NULL. Is this a feature I can set?

Thanks

(I have tried the cascaseremove on onDelete CASCADE option but the reference record was still just having the value set to NULL)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions