When establishing a ManyToOne/OneToMany relation, we are required to use @ManyToOne/@OneToMany decorators on a field.
In the context of my project, I have defined two entities: Project
and Position
.
This is how the relation was set up:
@Entity('positions')
export class Position {
@ManyToOne(() => Project, {
nullable: false,
eager: true,
})
@JoinColumn()
project: Project;
}
The TypeORM documentation states that this code will generate a projectId FOREIGN KEY
column in the database to store the project id.
Now, when attempting to access the project
property, TypeORM loads the corresponding project based on the id stored in the projectId
field.
QUERY
Is there a way to retrieve the value of the pojectId
field without loading the relational entity?
By default, the projectId
property does not exist within the Position
entity. Even if manually created, it remains unpopulated with the value from the projectId
column.
I attempted to approach it using the following method:
@ManyToOne(() => Project, {
nullable: false,
eager: false,
})
@JoinColumn()
project: Project;
projectId: string;