Typeorm: Problem when eager fetching realtionship in many-to-many

Created on 27 Oct 2017  路  1Comment  路  Source: typeorm/typeorm

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.

question

Most helpful comment

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;
}

>All comments

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;
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

leixu2txtek picture leixu2txtek  路  3Comments

guscastro picture guscastro  路  3Comments

shroudedcode picture shroudedcode  路  3Comments

CocaColaBear picture CocaColaBear  路  3Comments

juanjalvarez picture juanjalvarez  路  3Comments