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);
M2N relationships are typically solved with a separate table listing combinations of PK of both entities. If this applies to TypeOrm I cannot say