typeorm: Many to Many (bidirectional) - table path is undefined (bug in documentation?)
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.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 10
- Comments: 22 (1 by maintainers)
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.@mregula I don’t think
role => role.usershas anything to do with auto-generating/naming the join table.Have you tried:
@mregula I believe your problem is on user
@ManyToMany. It should look something like@ManyToMany(type => Role, role => role.users)For future readers: I accidentally used
JoinColumninstead ofJoinTableon my Many-To-Many relation.Debug context
Just ran into this “problem”. I can confirm, the bi-directional many-to-many only works in one way (from the owning side specified with
@JoinTable), the other way throws an error.I tried to do a little digging into the problem. The problem is that TypeOrm is trying to access the
relation.junctionEntityMetadata.tablePathproperty.junctionEntityMetadataisundefined, however, it is available underrelation.inverseRelation.junctionEntityMetadata.Perhaps it there is missing logic to detect an inverse relation? I’m not familiar with the workings of TypeOrm and wouldn’t want to break anything.
Koa
2.8.1, Typeorm0.2.18, Node12.9.0, Postgres11.5Throwing line: https://github.com/typeorm/typeorm/blob/d1594f5d41a35bac7bb99d4f00e76c2fb670ee17/src/query-builder/SelectQueryBuilder.ts#L1467
The thrown `Error`
Entity objects
Also tried removing custom table names and re-creating all tables.
Film (owning relation)
Key
The `relation` object (contains the `undefined` property)
This was dumped just before the error is thrown (see the link above).
Edit 2019-10-25: Fixed link to point to a specific commit line number, not master branch
It is a serious problem because if I add @JoinTable decorator on both sides, insert statements are spoiled. With @JoinTable decorator only on one side, I can start query only from one entity and relation is not bidirectional anymore. Could you please refer to this issue? Thanks.
right,
@JoinTablemust be only from one side of relation. Your problem I guess is that you are using@JoinTablewrongly. Docs clearly state you to usereferenceColumnName. I don’t see it in your code example.This did the trick for me, thanks!