How to make an Insert in the database from a many to many relationship in typeorm

Asked

Viewed 66 times

0

I am developing a professional register where each professional can speak one or more languages, however when saving professionals, languages are not saved

@Entity('idioma')
export class Idioma {
  @PrimaryGeneratedColumn()
  id: number;
  @Column()
  lang: string;
  @Column()
  native: boolean;
}
@Entity('Profissional')
export class Profissional {
  @PrimaryGeneratedColumn()
  id: number;
  @Column()
  name: string;
  @Column()
  @IsOptional()
  phone: string;
  @Column()
  @IsOptional()
  celPhone: string;
  @Column()
  @IsOptional()
  sexo: string;
  @Column()
  @IsOptional()
  nasc: Date;
  @Column('bool')
  @IsOptional()
  fumante: boolean;
  @Column()
  @IsOptional()
  ocupation: string;
  @Column()
  @IsEmail()
  email: string;
  @OneToMany(
    () => Idioma,
    idioma => idioma.id,
    {cascade: true}
  )
  lang: Idioma[];
  @Column()
  password: string;
  @Column('decimal', { precision: 5 })
  latitude: number;
  @Column('decimal', { precision: 5 })
  longitude: number;
  @Column('decimal', { precision: 2 })
  @IsOptional()
  workPrice: number;
}
async create(profissional: ProfissionalDto) {
    const errors = validate(profissional);
    
    
    profissional.password = bcrypt.hashSync(profissional.password, 12);
    const prof = this.profissionalRepository.save(profissional);
    
    return prof;
  }

1 answer

0


What is missing there is that in the entity Idiom, a declaration of relationships is necessary, as can be seen in documentation:

You can omit @Joincolumn in a @Manytoone / @Onetomany relation. @Onetomany cannot exist without @Manytoone. If you want to use @Onetomany, @Manytoone is required. However, the inverse is not required: If you only care about the @Manytoone Relationship, you can define it without having @Onetomany on the Related Entity. Where you set @Manytoone - its Related Entity will have "relation id" and Foreign key.


You can omit @Joincolumn in a @Manytoone/ @Onetomany relation. @Onetomany cannot exist without @Manytoone. If you want to use @Onetomany, @Manytoone is required. However, the reverse is not necessary: if you only care about the @Manytoone relationship, you can define it without having @Onetomany in the related entity. Where you define @Manytoone - your related entity will have "relation id" and foreign key.

I mean, you used @OneToMany() in the Professional entity, it is mandatory to declare @ManyToOne() in Language, as the Typeorm assembles all the logic in the database depending on this relationship, as can be seen above, if you want you can only declare @Manytoone() and omit @Onetomany() that you will have no problems:

@Entity('idioma')
export class Idioma {
  @PrimaryGeneratedColumn()
  id: number;
  @Column()
  lang: string;
  @Column()
  native: boolean;
  @ManyToOne(() => Profissional, profissional => profissional.lang)
  profissional: Profissional;
}

@Entity('Profissional')
export class Profissional {
  @PrimaryGeneratedColumn()
  id: number;
  ...
  @OneToMany(() => Idioma, idioma => idioma.id, {
    cascade: true
  })
  lang: Idioma[];
  ...
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.