Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

292
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!