【问题标题】:TypeORM relation through third table won't work通过第三个表的 TypeORM 关系不起作用
【发布时间】: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


    【解决方案1】:

    您的 OrganizationEntity 在 OneToMany 配置中有错误;您设置了userRole.user,但它应该是userRole.organization,这是该表中链接到该字段的字段。

        @OneToMany(() => UserRoleEntity, userRole => userRole.organization)
        users!: UserRoleEntity[]
    

    此外,您应该向表中添加主键。默认情况下,一种安全的方法是使用 Generated 列。您可以将其添加到您的 3 个表中。对于多对多表,您可以创建一个复合键,或者只是一个带有 user_id 和 company_id 的索引,但需要权衡 - 所以只需在每个表上使用一个 id 即可开始。

        @PrimaryGeneratedColumn()
        id: string;
    

    【讨论】:

    • 谢谢,被接受为有用的答案。虽然 - 仍然没有任何东西被提取到 users 数组中 - 响应看起来像这样:prnt.sc/1366qie。仔细检查了数据库 - 它包含一些应该出现在其中一个数组中的数据。如果我仍然做错了什么,请告诉我。
    • 哦,你的表有 id 列吗?我会在组织和用户中添加 PrimaryGeneratedColumn()。那么如果sql生成正确,你只需要查看你的数据,并确保UserRoleEntity中有其organization_id等于organization.id值的行。
    • 是的,它们都有一个 id 字段。
    • 呵呵,抱歉——是我在数据库中做了一些更改并保存了它们,但它们没有被提交。谢谢。
    • 不错!为了其他读者的清楚,我将编辑我的答案!
    猜你喜欢
    • 2018-08-19
    • 2016-05-08
    • 2021-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 2017-06-25
    • 1970-01-01
    相关资源
    最近更新 更多