Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

289
Vistas
How to save bi-directional many to many relationships in TypeORM

I have two tables that are related to each other in a bi-directional many to many relationship. There is a User and a Group table. If a group is created it should be saved to the join table, but that isn't happening. The Group is being created but the join table isn't being updated.

export class User {
  @PrimaryGeneratedColumn()
  id!: number;

  @ManyToMany(() => Group, (group) => group.users, {
    cascade: true,
  })
  @JoinTable()
  groups: Group[];
}

export class Group {
  @PrimaryGeneratedColumn()
  id!: number;

  @ManyToMany(() => User, (user) => user.groups)
  users: User[];
}
const u = await this.userRepository.findOne({
  where: {
    id: user.id,
  },
});

const newGroup = new Group();
newGroup.name = name;
await this.connection.manager.save(newGroup);

u.firstName = 'randy';
u.groups.push(newGroup);
await this.connection.manager.save(u);
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

M2N relationships are typically solved with a separate table listing combinations of PK of both entities. If this applies to TypeOrm I cannot say

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda