【发布时间】:2021-05-20 13:39:22
【问题描述】:
按照 TypeORM 教程(这部分 - https://typeorm.io/#/many-to-many-relations/many-to-many-relations-with-custom-properties)我在我的应用程序中创建了模型:
@Entity({ name: 'organizations'})
export class OrganizationEntity extends AbstractEntity {
@Column()
name: string;
@Column({ nullable: true })
description: string;
@Column({ nullable: true })
logo: string;
@ManyToOne(() => UserEntity, { nullable: true })
mainContactUser: UserEntity;
@OneToOne(() => LicenseEntity, license => license.organization)
license: LicenseEntity;
@OneToMany(() => UserRoleEntity, userRole => userRole.user)
users!: UserRoleEntity[]
}
@Entity({ name: 'users' })
export class UserEntity extends AbstractEntityWithDates {
@Column({ nullable: false })
firstName: string;
@Column({ nullable: false})
lastName: string;
@Column({ nullable: true })
jobTitle: string;
@Column({ unique: true })
email: string;
@Column({ nullable: true })
phone: string;
@Column({ nullable: true })
avatar: string;
@Column({ nullable: true })
oAuthIdentityId: string;
@OneToMany(() => UserRoleEntity, userRole => userRole.user)
userRoles: UserRoleEntity[]
}
最后:
@Entity({ name: 'user_roles' })
export class UserRoleEntity extends AbstractEntity {
@ManyToOne(() => UserEntity, user => user.userRoles)
user: UserEntity;
@ManyToOne(() => OrganizationEntity, organization => organization.users, { nullable: true })
organization: OrganizationEntity;
@ManyToOne(() => ProjectEntity, { nullable: true })
project: ProjectEntity;
@Column({
type: "enum",
enum: RoleType,
nullable: true
})
role: string;
}
然后在一项服务中,我正在执行一个查询,我想在其中获取至少具有相关用户角色的所有组织:
const found = await this.organizationRepository
.createQueryBuilder("organization")
.leftJoinAndSelect("organization.users", "users")
.getMany();
没有错误但是TypeORM生成的查询如下:
SELECT "organization"."id" AS "organization_id", "organization"."name" AS "organization_name", "organization"."description" AS "organization_description", "organization"."logo" AS "organization_logo", "organization"."main_contact_user_id" AS "organization_main_contact_user_id", "users"."id" AS "users_id", "users"."role" AS "users_role", "users"."user_id" AS "users_user_id", "users"."organization_id" AS "users_organization_id", "users"."project_id" AS "users_project_id"
FROM "organizations" "organization"
LEFT JOIN "user_roles" "users" ON "users"."user_id"="organization"."id"
这部分:
...ON "users"."user_id"="organization"."id"
显然不正确,但我不明白为什么?
任何帮助将不胜感激。
【问题讨论】:
标签: typeorm