Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RelationIdLoader is not consistently respecting maxAliasLength #11227

Open
2 of 18 tasks
te1 opened this issue Jan 7, 2025 · 0 comments · May be fixed by #11228
Open
2 of 18 tasks

RelationIdLoader is not consistently respecting maxAliasLength #11227

te1 opened this issue Jan 7, 2025 · 0 comments · May be fixed by #11228

Comments

@te1
Copy link

te1 commented Jan 7, 2025

Issue description

RelationIdLoader is using DriverUtils.buildAlias in some places but not in others. Notably this causes relationLoadStrategy: 'query' to fail while 'join' works for OneToOne relations that generate very long aliases.

Expected Behavior

RelationIdLoader should consistently use DriverUtils.buildAlias when generating column aliases that fit the maxAliasLength prop of driver.

Actual Behavior

loadForManyToOneAndOneToOneOwner ignores maxAliasLength for the hasAllJoinColumnsInEntity case. This causes id mismatches later in loadManyToManyRelationIdsAndGroup which uses DriverUtils.buildAlias.

Steps to reproduce

Applies to PostgreSQL, MySQL and possibly other drivers

Schema

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

    @Column()
    superLongRelatedEntityNameDontAskWhy_id: number

    @OneToOne(() => SuperLongRelatedEntityNameDontAskWhy)
    @JoinColumn({ name: "superLongRelatedEntityNameDontAskWhy_id" })
    superLongRelatedEntityNameDontAskWhy: SuperLongRelatedEntityNameDontAskWhy
}

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

    @Column()
    name: string
}

Test Case

const related = await connection
    .getRepository(SuperLongRelatedEntityNameDontAskWhy)
    .save({ name: "test" })

const entity = await connection.getRepository(SomeEntity).save({
    superLongRelatedEntityNameDontAskWhy_id: related.id,
})

const result = await connection.getRepository(SomeEntity).findOne({
    where: { id: entity.id },
    relations: { superLongRelatedEntityNameDontAskWhy: true },
    relationLoadStrategy: "query",
})

// result.superLongRelatedEntityNameDontAskWhy is null

My Environment

Dependency Version
Operating System Windows 10 Pro 22H2
Node.js version 18.20.4
Typescript version 5.7.2
TypeORM version 0.3.20

Additional Context

This is a follow up to #9379 / #9380 which didn't consider this specific case.

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.

te1 added a commit to te1/typeorm that referenced this issue Jan 7, 2025
Update RelationIdLoader to use DriverUtils.getAlias to prevent aliases being possibly trimmed by database.

Closes: typeorm#11227
@te1 te1 linked a pull request Jan 7, 2025 that will close this issue
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant