Repository navigation
Closed
Labels
Description
Issue description
Nested relations with an eager parent aren't fetched
Expected Behavior
I would expect that when I specify a sub relation within the tree I'd be able to get it although its parent is eager.
Actual Behavior
At the moment the sub relation isn't loaded at all.
Steps to reproduce
/* ParentEntity.ts. */
@Entity({ name: 'ParentEntity' })
export class ParentEntity extends BaseEntity {
@ManyToOne(() => Child, (child) => child.parents, { eager: true, nullable: true })
@JoinColumn()
child: Relation<Child>;
@RelationId((parent: Parent) => parent.child)
childId: string;
}
/* ChildEntity.ts */
@Entity({ name: 'ChildEntity' })
export class ChildEntity extends BaseEntity {
@OneToMany(() => Parent, (parent) => parent.child)
@JoinColumn()
parents: Relation<Parent[]>;
@OneToOne(() => ChildInfo, { nullable: true })
@JoinColumn()
info: Relation<ChildInfo>;
}
/* ChildInfoEntity */
@Entity({ name: 'ChildInfoEntity' })
export class ChildInfoEntity extends BaseEntity {
@OneToMany(() => Child, (child) => child.info)
@JoinColumn()
parents: Relation<Parent[]>;
@Column({ nullable: true })
name: string;
}
/* queries.ts */
export const getParentWithChildren(parentId: string) => {
return ParentEntity.find({
where: { id: parentId },
relations: ['child.info']
});
}
/* queries.test.ts */
describe('getParentWithChildren', () => {
beforeAll(() => {
seedDb();
})
it('should return parent with child info', async () => {
const parentWithChild = await getParentWithChildren('some-parent-id');
expect(partnerWithChild.child).toBeDefined(); // this will pass, we do get the child object itself.
expect(parentWithChild.child.info.name).toBeDefined(); // currently fails, info is not populated.
})
})My Environment
| Dependency | Version |
|---|---|
| Operating System | macOS Monterey |
| Node.js version | 16.15.0 |
| Typescript version | 4.7.4 |
| TypeORM version | 0.3.12 but also tried with 0.3.20 |
Additional Context
When getting rid of eager and specifying the nested relation (info in this case),
we do see the expected result.
P.S: The above example is a semi pseudo code, it tries to imitate what we do in production.
Relevant Database Driver(s)
- aurora-mysql
- aurora-postgres
- better-sqlite3
- cockroachdb
- cordova
- expo
- mongodb
- mysql
- nativescript
- oracle
- postgres
- react-native
- sap
- spanner
- sqlite
- sqlite-abstract
- sqljs
- sqlserver
Are you willing to resolve this issue by submitting a Pull Request?
Yes, I have the time, but I don't know how to start. I would need guidance.
Reacted by Giga Gatenashvili, Rajan Kadeval, Mackenzie Salisbury and Lorenzo Zabot
Activity
konrad-molitor commentedon Jun 17, 2024
Yep, just encountered this one.
Solved by adding
loadEagerRelations: falsefor now.github-actions commentedon Sep 18, 2025
TypeORM has returned to active development after recently transitioning to new leadership (see related announcement).
As part of the new team's efforts to prioritise work on the project moving forward, it is necessary to clean up the backlog of stale issues.
🧹 This issue is being automatically closed because it has had no activity in the last 6 months.
If you believe this issue is still relevant, feel free to comment below and the maintainers may reopen it. Thank you for your contribution.