typeorm ondelete option

◾️ CASCADE: if user is deleted, delete all related order info

@Entity()
export class Order extends CoreEntity {
  @Field((type) => User)
  @ManyToOne((type) => User, (user) => user.orders, {
    onDelete: 'CASCADE',
  })
  customer: User;
}

◾️ SET NULL: if user is deleted, do not delete the related order info

export class Order extends CoreEntity {
  @Field((type) => User, { nullable: true })
  @ManyToOne((type) => User, (user) => user.orders, {
    onDelete: 'SET NULL',
    nullable: true,
  })
  customer?: User;

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です