Skip to content

Embedded lazy relations throw at runtime when used with embedded entities #12223

@Cprakhar

Description

Issue description

Embedded lazy relations throw at runtime when used with embedded entities

Expected Behavior

  • Saving an entity with an embedded lazy relation assigned as Promise.resolve(relatedEntity) should persist the relation FK.
  • Accessing the lazy relation on a loaded entity should resolve to the related entity (not null/undefined).

Actual Behavior

TypeError: Cannot read properties of undefined (reading '__profile__')
      at RelationMetadata.getEntityValue (src/metadata/RelationMetadata.ts:456:35)
      at ColumnMetadata.getEntityValue (src/metadata/ColumnMetadata.ts:799:47)
      at InsertQueryBuilder.createColumnValueExpression (src/query-builder/InsertQueryBuilder.ts:1520:28)
      at /home/ghostblasius/typeorm/src/query-builder/InsertQueryBuilder.ts:875:40
      at Array.forEach (<anonymous>)
      at /home/ghostblasius/typeorm/src/query-builder/InsertQueryBuilder.ts:858:25
      at Array.forEach (<anonymous>)
      at InsertQueryBuilder.createValuesExpression (src/query-builder/InsertQueryBuilder.ts:857:23)
      at InsertQueryBuilder.createInsertExpression (src/query-builder/InsertQueryBuilder.ts:443:39)
      at InsertQueryBuilder.getQuery (src/query-builder/InsertQueryBuilder.ts:43:21)
      at InsertQueryBuilder.getQueryAndParameters (src/query-builder/QueryBuilder.ts:491:28)
      at InsertQueryBuilder.execute (src/query-builder/InsertQueryBuilder.ts:161:50)
      at SubjectExecutor.executeInsertOperations (src/persistence/SubjectExecutor.ts:446:26)
      at SubjectExecutor.execute (src/persistence/SubjectExecutor.ts:137:20)
      at EntityPersistExecutor.execute (src/persistence/EntityPersistExecutor.ts:182:36)
      at processTicksAndRejections (node:internal/process/task_queues:104:5)
      ...

Steps to reproduce

export class Author {
    @Column()
    name: string

    @OneToOne(() => Profile)
    @JoinColumn()
    profile: Promise<Profile>
}

@Entity()
export class Post {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    title: string

    @Column(() => Author)
    author: Author
}

@Entity()
export class Profile {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    about: string
}
  1. Save a related entity (Profile).
  2. Create a Post with embedded Author and assign lazy relation:
    • post.author.profile = Promise.resolve(profile)
  3. Save post.
  4. Load post with findOne and access lazy relation:
    • await loadedPost.author.profile
it("should lazy load embedded relations", () =>
        Promise.all(
            dataSources.map(async (dataSource) => {
                const profile = new Profile()
                profile.about = "I am John Doe profile"
                await dataSource.manager.save(profile)

                const post = new Post()
                post.title = "Post with embedded author"
                post.author = new Author()
                post.author.name = "John Doe"
                post.author.profile = Promise.resolve(profile)
                await dataSource.manager.save(post)

                const loadedPost = await dataSource.manager.findOne(Post, {
                    where: { title: "Post with embedded author" },
                })

                const lazyLoadedProfile = await loadedPost!.author.profile
                lazyLoadedProfile.about.should.be.equal("I am John Doe profile")

                const loadedPostForCreate = await dataSource.manager.findOne(
                    Post,
                    {
                        where: { title: "Post with embedded author" },
                    },
                )
            }),
        ))

My Environment

Dependency Version
Operating System Linux
Node.js version v24.14.0
Typescript version 5.9.3
TypeORM version 1.0.0-pre

Additional Context

No response

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, and I know how to start.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions