TypeORM getRepository.find()不包括外键字段

TypeORM getRepository.find()不包括外键字段,orm,typeorm,ts-node,Orm,Typeorm,Ts Node,我正在尝试获取我的实体中包含的所有列,但我只能获取与其他实体没有任何关系的列 我使用这段代码将所有行提取到此存储库 private translationTextRepository = getRepository(TranslationText); async all(request: Request, response: Response, next: NextFunction) { return this.translationTextRepository.find(); }

我正在尝试获取我的
实体
中包含的所有列,但我只能获取与其他实体没有任何关系的列

我使用这段代码将所有行提取到此存储库

private translationTextRepository = getRepository(TranslationText);

async all(request: Request, response: Response, next: NextFunction) {
    return this.translationTextRepository.find();
}
@Entity('TranslationText')
export class TranslationText {

    @PrimaryGeneratedColumn()
    ID: number;

    @Column()
    CreatedBy: string;

    @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
    CreatedDate: Date;

    @Column()
    Status: boolean;

    @Column({ nullable: true, default: null })
    ModifiedBy: string;

    @Column({ type: 'timestamp', nullable: true, default: null })
    ModifiedDate: Date;

    @Column()
    Text: string;

    @ManyToOne((type) => Locale, (locale) => locale.ID)
    @JoinColumn({ name: 'LocaleID' })
    LocaleID: Locale;

    @ManyToOne((type) => TranslationTitle, (translationTitle) => translationTitle.ID)
    @JoinColumn({ name: 'TranslationTitleID' })
    TranslationTitleID: TranslationTitle;

}
这是这个存储库的
实体

private translationTextRepository = getRepository(TranslationText);

async all(request: Request, response: Response, next: NextFunction) {
    return this.translationTextRepository.find();
}
@Entity('TranslationText')
export class TranslationText {

    @PrimaryGeneratedColumn()
    ID: number;

    @Column()
    CreatedBy: string;

    @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
    CreatedDate: Date;

    @Column()
    Status: boolean;

    @Column({ nullable: true, default: null })
    ModifiedBy: string;

    @Column({ type: 'timestamp', nullable: true, default: null })
    ModifiedDate: Date;

    @Column()
    Text: string;

    @ManyToOne((type) => Locale, (locale) => locale.ID)
    @JoinColumn({ name: 'LocaleID' })
    LocaleID: Locale;

    @ManyToOne((type) => TranslationTitle, (translationTitle) => translationTitle.ID)
    @JoinColumn({ name: 'TranslationTitleID' })
    TranslationTitleID: TranslationTitle;

}
但我只能获取所有列,除了
LocaleID
TranslationTitleID

我怎样才能做到这一点

检查此文档: 解决方案:

  • 定义新列:

    @列()
    LocaleID:number

  • 将旧的重命名为:Locale

    但由于外键问题,typeOrm无法同步表

  • 在@ManyToOne({eager:true})中使用eager选项

  • 搜索结果将包含关系区域设置对象,您可以从中获取id。

    您可以尝试指定如下关系:

    async all(request: Request, response: Response, next: NextFunction) {
       return this.translationTextRepository.find({
         relations:["LocaleID","TranslationTitleID"]
        });
    }
    

    因为您必须明确表示希望在查询中显示您的关系。

    2020年12月,我准备让#1工作。我必须做的唯一修改是修改不可为null的ID列的种子文件。从
    this.user=context?.user
    this.userId=context?.user?.id
    。定义id列优于加载或定义关系。仅获取FK(与所有字段相比)可提高性能。此外,当您尝试保存包含嵌套对象的模型时,完整查询可能会导致错误:服务器可能会尝试覆盖数据库中的这些嵌套对象。明确定义FK id可以为您提供最多的控制。此外,id列使查找变得更容易。比较
    let-findOpt={where:{user:{id:'asdf'}}
    vs
    let-findOpt={where:{userId:'asdf'}}}
    ..
    等待ModelClass.find(findOpt)