查找调用中未显示 TypeORM 外键 [英] TypeORM foreign key not showing on a find call

查看:25
本文介绍了查找调用中未显示 TypeORM 外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

export class Contact extends BaseEntity {
  ...
  @ManyToOne(() => User, { nullable: false })
  @JoinColumn({ name: 'user_id' })
  user: User;
  ...
}

const repo = new Repository<User> ();
const response = await repo.findAll();
console.log(response);

console.log:
[
 Contact {
  id: 1,
  ...
 },
 Contact {
  id: 2,
  ...
 }
]

我正在尝试获取包含在我的 Contact 中的所有列,但我只能获取与其他实体没有任何关系的列.它不包括 user_id 列.例如为什么不能获取外键?

I am trying to fetch all the columns included on my Contact, but I only able to fetch the columns that does not have any relationship from the other entity. It does not include user_id columns. Why can't to get foreign key for instance?

推荐答案

export class Contact extends BaseEntity {
  ...
  // add column explicitly here
  @Column({ name: 'user_id' })
  userId: number;

  @ManyToOne(() => User, { nullable: false })
  @JoinColumn({ name: 'user_id' })
  user: User;
  ...
}

您应该显式添加 userId 列并将此列名称传递给 @JoinColumn 装饰器.

You should add userId column explicitly and pass this column name to @JoinColumn decorator.

希望有帮助.

这里是关于它的讨论.https://github.com/typeorm/typeorm/issues/586#issuecomment-311282863

这篇关于查找调用中未显示 TypeORM 外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆