Skip to content

RelationIdLoader is not respecting maxAliasLength #9379

Closed
antz-snyk1/gradle-npm-monorepo-example
#5

Description

@janzipek

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)

Activity

  1. added 3 commits that reference this issue on Sep 16, 2022
    1c3a969
    88c5565
    2dd3d8b
  2. added a commit that references this issue on Sep 19, 2022
    a917d65
  3. depyronick commented on Nov 21, 2022

    @depyronick

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

  4. depyronick commented on Nov 21, 2022

    @depyronick

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

  5. depyronick commented on Nov 26, 2022

    @depyronick

    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

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