TypeORM foreign key not showing on a find call

Viewed 6568
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,
  ...
 }
]

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?

2 Answers
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;
  ...
}

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

Hope it helps.

Here is discussion about it. https://github.com/typeorm/typeorm/issues/586#issuecomment-311282863

Related