Many-to-Many with custom fields

See original GitHub issue

Hi guys I was wandering if its possible to have a many-to-many relationship where I can add some custom fields to the created table. I was looking at previous issues and someone told to create the other table and use one-to-many relationship. However I’m not understanding how I would acomplish that where then I would do something like: const user = await this.userRepository.findOne(id) and get the result as a many-to-many.

Something on this lines: User: n------ n :Group

And get the output as:

{ "id": 1, "firstName": "firstname", "lastName": "lastname", "groups": [ { "id": 1, "name": "Group1" } ] } Behind this there is the table user_group that makes this connection between User and Groups and has for example a field called isDeleted that helps me to know is this was soft deleted and not show this entry.

Could you guys help me on the right path?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:30 (8 by maintainers)

github_iconTop GitHub Comments

126reactions
pleerockcommented, Dec 1, 2017

Something like this is to implement “additional columns in many-to-many table”

@Entity()
export class User {
    @PrimaryGeneratedColumn()
    id: number;
    @Column()
    name: string;
    @OneToMany(type => UserGroup, userGroup => userGroup.user)
    userGroups: UserGroup[];
}
@Entity()
export class UserGroup {
    @Column()
    isActive: boolean;
    @ManyToOne(type => User, user => user.userGroups, { primary: true })
    user: User;
    @ManyToOne(type => Group, group => group.userGroups, { primary: true })
    group: Group;
}
@Entity()
export class Group {
    @PrimaryGeneratedColumn()
    id: number;
    @Column()
    name: string;
    @OneToMany(type => UserGroup, userGroup => userGroup.group)
    userGroups: UserGroup[];
}

Not sure if I understood your other requests

53reactions
josephbuchmacommented, Jun 28, 2019

You can specify custom join table for ManyToMany relation

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number

  @Column()
  name: string

  @OneToMany(type => UserGroup, userGroup => userGroup.user)
  userGroups: UserGroup[];

  @ManyToMany(type => Group, group => group.users)
  @JoinTable({
    name: 'user_groups',
    joinColumn: {
      name: 'user_id',
      referencedColumnName: 'id',
    },
    inverseJoinColumn: {
      name: 'group_id',
      referencedColumnName: 'id',
    },
  })
  groups: Group[]
}

@Entity('user_groups')
export class UserGroup {
  @Column()
  isActive: boolean

  @JoinColumn("user_id")
  @ManyToOne(type => User, user => user.userGroups, { primary: true })
  user: User

  @JoinColumn("group_id")
  @ManyToOne(type => Group, group => group.userGroups, { primary: true })
  group: Group
}

@Entity()
export class Group {
  @PrimaryGeneratedColumn()
  id: number

  @Column()
  name: string

  @ManyToMany(type => User, user => user.groups)
  users: UserGroup[]

  @OneToMany(type => UserGroup, userGroup => userGroup.group)
  userGroups: UserGroup[];
}

Read more comments on GitHub >

github_iconTop Results From Across the Web

typeorm - how to add extra field to "many to many" table?
It is not possible to add custom column in the auto created many-to-many bridge table. So create another table and give one-to-many and ......
Read more >
How to create a custom many to many relationship in TypeORM
createTable( new Table({ name: "heros", columns: [ { name: "id", type: "int", isPrimary: true, generationStrategy: "increment", }, { name: "name ...
Read more >
ManyToMany with Extra Fields > Doctrine Collections
You see, ManyToMany relationships only work when you have no extra fields on the relationship. But don't worry! That's by design! As soon...
Read more >
Many to Many Relationship with Extra Field - Laracasts
Many to Many Relationship with Extra Field ... this i went the common route of creating a pivot table and use Laravels many...
Read more >
Many-to-many relations - typeorm - GitBook
Many-to-many is a relation where A contains multiple instances of B, and B contain multiple instances of A. Let's take for example Question...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found