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 respecting maxAliasLength #9379

Closed
janzipek opened this issue Sep 16, 2022 · 3 comments · Fixed by #9380 · May be fixed by antz-snyk1/gradle-npm-monorepo-example#5
Closed

RelationIdLoader is not respecting maxAliasLength #9379

janzipek opened this issue Sep 16, 2022 · 3 comments · Fixed by #9380 · May be fixed by antz-snyk1/gradle-npm-monorepo-example#5

Comments

@janzipek
Copy link
Contributor

janzipek commented Sep 16, 2022

Issue Description

RelationIdLoader class is doing selects, but it's not using DriverUtils.buildAlias so the selects can end up being truncated and all related behaviour is broken. This for example breaks relationLoadStrategy: 'query' when the relation key ends up being too long.

Expected Behavior

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

Actual Behavior

RelationIdLoader ignores maxAliasLength.

Steps to Reproduce

(in postgresql)

Schema

@Entity()
export class SuperLongTableName {
    @PrimaryGeneratedColumn()
    id: number
    
    @Column()
    name: string
    
    @OneToMany(() => SuperLongTableNameWhichIsRelatedToOriginalTable, table => table.superLongTableName)
    relatedToOriginal: SuperLongTableNameWhichIsRelatedToOriginalTable[]
}

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

    @Column()
    superLongTableNameId: number;
    
    @ManyToOne(() => SuperLongTableName, table => table.relatedToOriginal)
    @JoinColumn({ name: 'superLongTableNameId' })
    superLongTableName: SuperLongTableName
}
INSERT INTO "SuperLongTableName" ("name") VALUES ('test');
INSERT INTO "SuperLongTableNameWhichIsRelatedToOriginal" ("superLongTableNameId") VALUES (1);
const data = await appDataSource.getRepository(SuperLongTableName).findOne({
  where: { id: 1 },
  relations: { relatedToOriginal: true }
})

// data = { name: 'test', relatedToOriginal: [] }

This is because RelationIdLoader will generate column with name: SuperLongTableNameWhichIsRelatedToOriginal_relatedToOriginal_superLongTableNameId which is then truncated by database and all the conditions in the code fail because of that.

Relevant issues

#3118

My Environment

Dependency Version
Operating System
Node.js version 14.19.1
Typescript version 4.8.2
TypeORM version 0.3.7

Relevant Database Driver(s)

DB Type Reproducible
aurora-mysql no
aurora-postgres no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql no
nativescript no
oracle no
postgres yes
react-native no
sap no
spanner no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

Are you willing to resolve this issue by submitting a Pull Request?

  • ✖️ Yes, I have the time, and I know how to start.
  • ✅ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

(EDIT: Fixed the example, the names were too short)

janzipek pushed a commit to janzipek/typeorm that referenced this issue Sep 16, 2022
Update RelationIdLoader to use DriverUtils.getAlias to prevent aliases being possibly trimmed by database.

Closes: typeorm#9379
janzipek pushed a commit to janzipek/typeorm that referenced this issue Sep 16, 2022
Update RelationIdLoader to use DriverUtils.getAlias to prevent aliases being possibly trimmed by database.

Closes: typeorm#9379
janzipek pushed a commit to janzipek/typeorm that referenced this issue Sep 16, 2022
Update RelationIdLoader to use DriverUtils.getAlias to prevent aliases being possibly trimmed by database.

Closes: typeorm#9379
pleerock pushed a commit that referenced this issue Sep 19, 2022
Update RelationIdLoader to use DriverUtils.getAlias to prevent aliases being possibly trimmed by database.

Closes: #9379

Co-authored-by: Jan Zípek <jan@zipek.cz>
@depyronick
Copy link

I'm having propertyPath undefined error while using query relations. Is this related to that? Works with join but not with query. Mysql

@depyronick
Copy link

ok... it's because inverse side reference should be different than the referencedColumnName in my case

@depyronick
Copy link

but seriously, any chance this should cause problems?

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants