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

Ric*_*ich 12 orm typeorm ts-node

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

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

private translationTextRepository = getRepository(TranslationText);

async all(request: Request, response: Response, next: NextFunction) {
    return this.translationTextRepository.find();
}
Run Code Online (Sandbox Code Playgroud)

entity是这个存储库的。

@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;

}
Run Code Online (Sandbox Code Playgroud)

但是我只能获取除LocaleID和之外的所有列TranslationTitleID

我怎样才能做到这一点?

cha*_*les 7

检查此文档:https : //typeorm.io/#/relations-faq/how-to-use-relation-id-without-joining-relation 解决方案:

  1. 定义新列:

    @column()
    LocaleID: 数字

将旧的重命名为:Locale

但是由于外键问题,typeOrm 无法同步您的表。

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

搜索结果将包含关系 Locale 对象,您可以从中获取 id。


Ale*_*ira 5

你能尝试这样指定关系吗:

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

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