Skip to content

Nested relations with an eager parent aren't fetched #10812

Closed

Description

@naorpeled

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.

Activity

  1. konrad-molitor commented on Jun 17, 2024

    @konrad-molitor

    Yep, just encountered this one.
    Solved by adding loadEagerRelations: false for now.

  2. github-actions commented on Sep 18, 2025

    @github-actions

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions