Many to Many (bidirectional) - table path is undefined (bug in documentation?)

See original GitHub issue

Issue type:

[ ] question [x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [x] mysql / mariadb [ ] oracle [ ] postgres [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem: User entity

@Entity()
export class User {
  @PrimaryGeneratedColumn({
    type: "bigint",
    unsigned: true
  })
  id: number;

  @Column("varchar")
  name: string;

  @ManyToMany(type => Role)
  @JoinTable({
    name: "user_role",
    joinColumns: [{ name: "userId" }],
    inverseJoinColumns: [{ name: "roleId" }]
  })
  roles: Role[];
}

Role entity

@Entity()
export class Role {
  @PrimaryGeneratedColumn({
    type: "bigint",
    unsigned: true
  })
  id: number;

  @Column("varchar")
  name: string;

  @ManyToMany(type => User, user => user.roles)
  users: User[];

  @CreateDateColumn()
  createdAt: string;

  @UpdateDateColumn()
  updatedAt: string;
}

Then query:

db
    .getRepository(Role)
    .createQueryBuilder("role")
    .leftJoin("role.users", "user")
    .where("user.id = :id", { id: 1 })
    .getMany();

Error: TypeError: Cannot read property ‘tablePath’ of undefined

Everything is done the same as in documentation: http://typeorm.io/#/many-to-many-relations If I add @JoinTable to the “Role” entity, everything is working fine, but documentation cleary says: “We just made our relation bi-directional. Note, the inverse relation does not have a @JoinTable. @JoinTable must be only on one side of the relation.”

What is more, when the @JoinTable decorator is on both sides, it spoils inserting many to many:

db
 .createQueryBuilder()
 .relation(User, "roles")
 .of(2) //User id
 .add(10); // Role id

It produces query: ‘INSERT INTO user_role(userId, roleId) VALUES (10, 2)’ Foreign keys are switched.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:9
  • Comments:22 (1 by maintainers)

github_iconTop GitHub Comments

27reactions
matzmzcommented, Mar 14, 2019

you need to replace the following line (in the User entity) @ManyToMany(type => Role) with: @ManyToMany(type => Role, role => role.users)

21reactions
tizzyapunktcommented, Oct 24, 2019

I had exactly the same problem.

This is how it works for me:

First side of the relationship, Product Entity

  @ManyToMany(() => Showcase, (showcase) => showcase.products)
  @JoinTable()
  showcases: Showcase[];

Second side of the relationship, Showcase Entity

  @ManyToMany(() => Product, (product) => product.showcases )
  products: product[];

Make sure you do not forget both inverseSide functions (showcase) => showcase.products and (product) => product.showcases. Forgetting one of those caused the problem for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

cannot read properties of undefined (reading 'tablepath')
Issue Description. Bi-directional relationship between tables with the many-to-many relations not working with Javascript. Values are updated in the owner table ...
Read more >
Doctrine2 bidirectional manyToMany - Undefined index
The manyToMany in your dish entity says it is mappedBy: dish . However, it is in fact mapped by favouriteDishes .
Read more >
Hibernate ORM 5.2.18.Final User Guide - Red Hat on GitHub
However, unlike many other persistence solutions, Hibernate does not hide the power of SQL from ... you need Java 1.7 due to a...
Read more >
Working with Associations - Doctrine Object Relational ...
Doctrine Object Relational Mapper Documentation: Working with Associations. ... A property with a reference to many entities has to be instances of the ......
Read more >
Adding the ManyToOne Relation - SymfonyCasts
Now, it explains the four different types of relationships that exist in Doctrine: ManyToOne, OneToMany, ManyToMany and OneToOne. If you're not sure which ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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