MikroORM 无法映射使用包含复合外键的复合主键的连接表

问题描述

将具有复合主键(包括外键)的连接表映射到复合主键时出错。我认为问题出在 UserRole.user 上。

堆栈跟踪

TypeError: Cannot read property 'id' of undefined
    at /Users/emurphy/projects/aperture/aap-api/node_modules/@mikro-orm/core/utils/Utils.js:369:29
    at Array.map (<anonymous>)
    at Function.getorderedPrimaryKeys (/Users/emurphy/projects/aperture/aap-api/node_modules/@mikro-orm/core/utils/Utils.js:367:33)
    at /Users/emurphy/projects/aperture/aap-api/node_modules/@mikro-orm/core/utils/Utils.js:371:37
    at Array.map (<anonymous>)
    at Function.getorderedPrimaryKeys (/Users/emurphy/projects/aperture/aap-api/node_modules/@mikro-orm/core/utils/Utils.js:367:33)
    at /Users/emurphy/projects/aperture/aap-api/node_modules/@mikro-orm/core/utils/Utils.js:371:37
    at Array.map (<anonymous>)
    at Function.getorderedPrimaryKeys (/Users/emurphy/projects/aperture/aap-api/node_modules/@mikro-orm/core/utils/Utils.js:367:33)
    at EntityFactory.findEntity (/Users/emurphy/projects/aperture/aap-api/node_modules/@mikro-orm/core/entity/EntityFactory.js:95:35)

复制

创建以下实体:

@Entity()
export class Organization {
  @PrimaryKey({ type: 'uuid',defaultRaw: 'uuid_generate_v4()' })
  id: string = v4();

  @Unique()
  @Property({ columnType: 'varchar' })
  name: string;

  @OnetoMany({ entity: () => User,mappedBy: (user) => user.organization,cascade: [] })
  users = new Collection<User>(this);

  constructor(value: Partial<Organization> = {}) {
    Object.assign(this,value);
    this.users = this.users || new Collection<User>(this);
  }
}

@Entity()
export class User  {
  @PrimaryKey({ columnType: 'varchar' })
  id: string;

  @Index()
  @ManyToOne({
    entity: () => Organization,primary: true,wrappedReference: true,index: true,cascade: [],onDelete: 'no action',})
  organization: IdentifiedReference<Organization>;

  @Property({ columnType: 'varchar' })
  firstName: string;

  @Property({ columnType: 'varchar' })
  lastName: string;

  @Property({ columnType: 'varchar' })
  email: string;

  @OnetoMany({ entity: () => UserRole,mappedBy: (userRole) => userRole.user })
  userRoles = new Collection<UserRole>(this);

  [PrimaryKeyType]: [string,string];

  constructor(value: Partial<User> = {}) {
    Object.assign(this,value);
    this.userRoles = this.userRoles || new Collection<UserRole>(this);
  }
}

@Entity()
export class Role {
  @PrimaryKey({ columnType: 'varchar' })
  id: string;

  @Property({ columnType: 'varchar' })
  name: string;

  @OnetoMany({ entity: () => UserRole,mappedBy: (userRole) => userRole.role })
  userRoles = new Collection<UserRole>(this);

  constructor(value: Partial<Role> = {}) {
    Object.assign(this,value);
    this.userRoles = this.userRoles || new Collection<UserRole>(this);
  }
}

@Entity()
export class UserRole {
  @ManyToOne({
    entity: () => User,inversedBy: (x) => x.userRoles,onDelete: 'cascade',})
  user: Reference<User>;

  @ManyToOne({
    entity: () => Role,})
  role: IdentifiedReference<Role>;

  [PrimaryKeyType]: [string,string,string];

  constructor(value: Partial<UserRole> = {}) {
    Object.assign(this,value);
  }
}

运行以下任一查询

// query and map just the join table
await this.em.find(UserRole,{ user: { $eq: [userId,orgId] } })
// or try to populate it
await this.em.findOne(
  User,{ id: { $eq: userId },organization: { $eq: orgId } },{ populate: { userRoles: LoadStrategy.JOINED } },);

版本

依赖 版本
节点 14.15.3
打字稿 4.1.5
mikro-orm 4.4.4
mikro-orm/postgresql 4.4.4

我已尝试删除 UserRole.role 属性,但错误仍然存​​在。我尝试直接查询表并将其填充为查找另一个实体(用户)的一部分。我曾尝试使用查询构建器。我尝试使用普通实体而不是包装实体。我曾尝试对原始 em.map(...) 的结果使用 execute

是我设置错误还是这只是框架中的一个错误?我找不到这种特定场景的示例。

更新 该问题已在此处修复:https://github.com/mikro-orm/mikro-orm/issues/1624

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)