Hello,
I'm trying to eager load some data of a many to many relationship but I'm getting this error:
TypeError: Cannot read property 'tableName' of undefined
Here is the code for the entities:
@Entity()
class User extends BaseEntity {
@PrimaryGeneratedColumn()
id = undefined;
@ManyToMany((type) => Group, (group) => group.users)
groups = undefined;
}
@Entity()
class Group extends BaseEntity {
@PrimaryGeneratedColumn()
id = undefined;
@ManyToMany((type) => User, (user) => user.groups)
@JoinColumn()
users = undefined;
}
And here's the code I'm using to fetch the related information:
User.findOneById(1, { relations: ['groups'] });
I've investigated, and the problem seems to be in this line:
https://github.com/typeorm/typeorm/blob/master/src/query-builder/SelectQueryBuilder.ts#L1338
} else { // means many-to-many
const junctionTableName = relation.junctionEntityMetadata!.tableName;
Don't know why, but junctionEntityMetadata is undefined, hence causing the error.
I found your problem:
@Entity()
class Group extends BaseEntity {
@PrimaryGeneratedColumn()
id = undefined;
@ManyToMany((type) => User, (user) => user.groups)
// @JoinColumn() // WRONG! IT MUST BE FOLLOWING:
@JoinTable()
users = undefined;
}
Most helpful comment
I found your problem: