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?
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
You can set eager: true
in order to load User with the find command
@ManyToOne(() => User, { nullable: false, eager: true })
@JoinColumn({ name: 'user_id' })
user: User;
Link: https://orkhan.gitbook.io/typeorm/docs/relations
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With