Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

297
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda