Many to Many (bidirectional) - table path is undefined (bug in documentation?)
See original GitHub issueIssue 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:
- Created 5 years ago
- Reactions:9
- Comments:22 (1 by maintainers)
Top Related StackOverflow Question
you need to replace the following line (in the User entity)
@ManyToMany(type => Role)with:@ManyToMany(type => Role, role => role.users)I had exactly the same problem.
This is how it works for me:
First side of the relationship, Product Entity
Second side of the relationship, Showcase Entity
Make sure you do not forget both inverseSide functions
(showcase) => showcase.productsand(product) => product.showcases. Forgetting one of those caused the problem for me.