1

将具有包含外键的复合主键的连接表映射到复合主键时出现错误。我相信问题出在 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,
    primary: true,
    wrappedReference: true,
    cascade: [],
    onDelete: 'cascade',
  })
  user: Reference<User>;

  @ManyToOne({
    entity: () => Role,
    inversedBy: (x) => x.userRoles,
    primary: true,
    wrappedReference: true,
    cascade: [],
    onDelete: 'no action',
  })
  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
微生物 4.4.4
mikro-orm/postgresql 4.4.4

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

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

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

4

0 回答 0