Skip to content

Commit

Permalink
fix: Update RelationIdLoader to use DriverUtils.getAlias (#9380)
Browse files Browse the repository at this point in the history
Update RelationIdLoader to use DriverUtils.getAlias to prevent aliases being possibly trimmed by database.

Closes: #9379

Co-authored-by: Jan Zípek <[email protected]>
  • Loading branch information
janzipek and SkaceKamen authored Sep 19, 2022
1 parent 6bc723b commit a917d65
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 29 deletions.
77 changes: 48 additions & 29 deletions src/query-builder/RelationIdLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ColumnMetadata } from "../metadata/ColumnMetadata"
import { DataSource } from "../data-source/DataSource"
import { ObjectLiteral } from "../common/ObjectLiteral"
import { SelectQueryBuilder } from "./SelectQueryBuilder"
import { DriverUtils } from "../driver/DriverUtils"

/**
* Loads relation ids for the given entities.
Expand Down Expand Up @@ -158,11 +159,17 @@ export class RelationIdLoader {
return column.compareEntityValue(
relatedEntity,
relationId[
column.entityMetadata.name +
"_" +
relation.propertyPath.replace(".", "_") +
"_" +
column.propertyPath.replace(".", "_")
DriverUtils.buildAlias(
this.connection.driver,
column.entityMetadata.name +
"_" +
relation.propertyPath.replace(
".",
"_",
) +
"_" +
column.propertyPath.replace(".", "_"),
)
],
)
})
Expand Down Expand Up @@ -238,19 +245,23 @@ export class RelationIdLoader {

// select all columns from junction table
columns.forEach((column) => {
const columnName =
const columnName = DriverUtils.buildAlias(
this.connection.driver,
column.referencedColumn!.entityMetadata.name +
"_" +
column.referencedColumn!.propertyPath.replace(".", "_")
"_" +
column.referencedColumn!.propertyPath.replace(".", "_"),
)
qb.addSelect(mainAlias + "." + column.propertyPath, columnName)
})
inverseColumns.forEach((column) => {
const columnName =
const columnName = DriverUtils.buildAlias(
this.connection.driver,
column.referencedColumn!.entityMetadata.name +
"_" +
relation.propertyPath.replace(".", "_") +
"_" +
column.referencedColumn!.propertyPath.replace(".", "_")
"_" +
relation.propertyPath.replace(".", "_") +
"_" +
column.referencedColumn!.propertyPath.replace(".", "_"),
)
qb.addSelect(mainAlias + "." + column.propertyPath, columnName)
})

Expand Down Expand Up @@ -474,22 +485,26 @@ export class RelationIdLoader {
// select all columns we need
const qb = this.connection.createQueryBuilder()
relation.entityMetadata.primaryColumns.forEach((primaryColumn) => {
const columnName =
const columnName = DriverUtils.buildAlias(
this.connection.driver,
primaryColumn.entityMetadata.name +
"_" +
primaryColumn.propertyPath.replace(".", "_")
"_" +
primaryColumn.propertyPath.replace(".", "_"),
)
qb.addSelect(
mainAlias + "." + primaryColumn.propertyPath,
columnName,
)
})
relation.joinColumns.forEach((column) => {
const columnName =
const columnName = DriverUtils.buildAlias(
this.connection.driver,
column.referencedColumn!.entityMetadata.name +
"_" +
relation.propertyPath.replace(".", "_") +
"_" +
column.referencedColumn!.propertyPath.replace(".", "_")
"_" +
relation.propertyPath.replace(".", "_") +
"_" +
column.referencedColumn!.propertyPath.replace(".", "_"),
)
qb.addSelect(mainAlias + "." + column.propertyPath, columnName)
})

Expand Down Expand Up @@ -607,22 +622,26 @@ export class RelationIdLoader {
// select all columns we need
const qb = this.connection.createQueryBuilder()
relation.entityMetadata.primaryColumns.forEach((primaryColumn) => {
const columnName =
const columnName = DriverUtils.buildAlias(
this.connection.driver,
primaryColumn.entityMetadata.name +
"_" +
relation.inverseRelation!.propertyPath.replace(".", "_") +
"_" +
primaryColumn.propertyPath.replace(".", "_")
"_" +
relation.inverseRelation!.propertyPath.replace(".", "_") +
"_" +
primaryColumn.propertyPath.replace(".", "_"),
)
qb.addSelect(
mainAlias + "." + primaryColumn.propertyPath,
columnName,
)
})
relation.joinColumns.forEach((column) => {
const columnName =
const columnName = DriverUtils.buildAlias(
this.connection.driver,
column.referencedColumn!.entityMetadata.name +
"_" +
column.referencedColumn!.propertyPath.replace(".", "_")
"_" +
column.referencedColumn!.propertyPath.replace(".", "_"),
)
qb.addSelect(mainAlias + "." + column.propertyPath, columnName)
})

Expand Down
22 changes: 22 additions & 0 deletions test/github-issues/9379/entity/SuperLongTableName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
Entity,
PrimaryGeneratedColumn,
Column,
OneToMany,
} from "../../../../src"
import { SuperLongTableNameWhichIsRelatedToOriginalTable } from "./SuperLongTableNameIsRelatedToOriginal"

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

@Column()
name: string

@OneToMany(
() => SuperLongTableNameWhichIsRelatedToOriginalTable,
(table) => table.superLongTableName,
)
relatedToOriginal: SuperLongTableNameWhichIsRelatedToOriginalTable[]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
Entity,
PrimaryGeneratedColumn,
Column,
ManyToOne,
JoinColumn,
} from "../../../../src"
import { SuperLongTableName } from "./SuperLongTableName"

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

@Column()
superLongTableNameId: number

@ManyToOne(() => SuperLongTableName, (table) => table.relatedToOriginal)
@JoinColumn({ name: "superLongTableNameId" })
superLongTableName: SuperLongTableName
}
47 changes: 47 additions & 0 deletions test/github-issues/9379/issue-9379.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import "reflect-metadata"
import { expect } from "chai"
import { DataSource } from "../../../src/data-source/DataSource"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { SuperLongTableName } from "./entity/SuperLongTableName"
import { SuperLongTableNameWhichIsRelatedToOriginalTable } from "./entity/SuperLongTableNameIsRelatedToOriginal"

describe.only("github issues > #9379 RelationIdLoader is not respecting maxAliasLength", () => {
let connections: DataSource[]
before(
async () =>
(connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
enabledDrivers: ["postgres"],
})),
)
beforeEach(() => reloadTestingDatabases(connections))
after(() => closeTestingConnections(connections))

it("should fetch related entities properly", async () => {
for (const connection of connections) {
const origin = await connection
.getRepository(SuperLongTableName)
.save({ name: "test" })

await connection
.getRepository(SuperLongTableNameWhichIsRelatedToOriginalTable)
.save({
superLongTableNameId: origin.id,
})

const result = await connection
.getRepository(SuperLongTableName)
.findOne({
where: { id: origin.id },
relations: { relatedToOriginal: true },
relationLoadStrategy: "query",
})

expect(result?.relatedToOriginal.length).to.eq(1)
}
})
})

0 comments on commit a917d65

Please sign in to comment.