如何在Nest.js中的@ManyToMany关系中删除数据

时间:2020-03-18 20:37:27

标签: mysql nestjs typeorm

我在两个实体之间有一个ManyToMany关系

@Entity()
export class Device{

  @PrimaryColumn()
  Name:string;


  @Column({ nullable: true})
  Port:number;

  @Column({ nullable: true})
  IPadress:string;

  @Column({ nullable: true})
  Location:string;

  @ManyToMany(type => User)
    @JoinTable()
    users: User[];

  }

@Entity()
export class User{

  @PrimaryColumn()
  Id:number;
  @Column({ nullable: true})
  Departement:string;
  @Column({ nullable: true})
  FirstName:string;
  @Column({ nullable: true})
  LastName:string;
  @Column({ nullable: true})
  CardNumber:string;
  @Column({ nullable: true})
  MobilePhone:string;

}

我想知道如何与用户删除设备以及如何从设备中删除用户

1 个答案:

答案 0 :(得分:2)

我认为使您的实体关系双向有效:

https://graph.microsoft.com/IMAP.AccessAsUser.All

我想您已经设置了数据源,因此您可以通过以下方式仅删除用户:

props.put("mail.imap.sasl.enable", "true");
props.put("mail.imap.sasl.mechanisms", "XOAUTH2");
props.put("mail.imap.auth.login.disable", "true");
props.put("mail.imap.auth.plain.disable", "true");

,当您删除一台设备时,它将删除其所有关联用户:

@Entity()
export class Device{

  @PrimaryColumn()
  Name:string;    

  @Column({ nullable: true})
  Port:number;

  @Column({ nullable: true})
  IPadress:string;

  @Column({ nullable: true})
  Location:string;

  @ManyToMany(type => User, users => users.devices { cascade: true })
    @JoinTable()
    users: User[];

  }

@Entity()
export class User{

  @PrimaryColumn()
  Id:number;
  @Column({ nullable: true})
  Departement:string;
  @Column({ nullable: true})
  FirstName:string;
  @Column({ nullable: true})
  LastName:string;
  @Column({ nullable: true})
  CardNumber:string;
  @Column({ nullable: true})
  MobilePhone:string;
  @ManyToMany(type => Device, device => device.users)        
    devices: Device[];
}