将具有复合主键(包括外键)的连接表映射到复合主键时出错。我认为问题出在 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 |
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