Skip to content

Commit c6f608d

Browse files
authored
fix: resolve issue building eager relation alias for nested relations (#10004)
* fix: resolve issue building eager relation alias for nested relations Fix alias error for nested relations. When loading eager relations from nested levels only the first level was handled through the alias builder Closes: #9944 * fix: update test: issue-9944 to include postgres Add postgress as enabled driver for test "issue-9944" * fix: resolve issue building eager relations withput parsing for "." to get relations Fix alias error for nested relations by usinf relation.propertyName instead of relation.propertyPath By using property path, we would have to parse for "." to get the nested relations, but that could bring issued with column names that had dots within the name.
1 parent 78b2f48 commit c6f608d

File tree

10 files changed

+113
-24
lines changed

10 files changed

+113
-24
lines changed

src/driver/DriverUtils.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,11 @@ export class DriverUtils {
127127
buildOptions: { shorten?: boolean; joiner?: string } | undefined,
128128
...alias: string[]
129129
): string {
130-
const newAlias =
131-
alias.length === 1
132-
? alias[0]
133-
: alias.join(
134-
buildOptions && buildOptions.joiner
135-
? buildOptions.joiner
136-
: "_",
137-
)
130+
const joiner =
131+
buildOptions && buildOptions.joiner ? buildOptions.joiner : "_"
132+
133+
let newAlias = alias.length === 1 ? alias[0] : alias.join(joiner)
134+
138135
if (
139136
maxAliasLength &&
140137
maxAliasLength > 0 &&

src/find-options/FindOptionsUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ export class FindOptionsUtils {
391391
qb.connection.driver,
392392
{ joiner: "__" },
393393
alias,
394-
relation.propertyPath,
394+
relation.propertyName,
395395
)
396396

397397
// add a join for the relation

src/naming-strategy/DefaultNamingStrategy.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,6 @@ export class DefaultNamingStrategy implements NamingStrategyInterface {
209209
return prefix + tableName
210210
}
211211

212-
eagerJoinRelationAlias(alias: string, propertyPath: string): string {
213-
return alias + "_" + propertyPath.replace(".", "_")
214-
}
215-
216212
nestedSetColumnNames = { left: "nsleft", right: "nsright" }
217213
materializedPathColumnName = "mpath"
218214
}

src/naming-strategy/NamingStrategyInterface.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,6 @@ export interface NamingStrategyInterface {
162162
*/
163163
prefixTableName(prefix: string, tableName: string): string
164164

165-
/**
166-
* Gets the name of the alias used for relation joins.
167-
*/
168-
eagerJoinRelationAlias(alias: string, propertyPath: string): string
169-
170165
/**
171166
* Column names for nested sets.
172167
*/

test/github-issues/2200/naming/NamingStrategyUnderTest.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,4 @@ import { NamingStrategyInterface } from "../../../../src/naming-strategy/NamingS
33

44
export class NamingStrategyUnderTest
55
extends DefaultNamingStrategy
6-
implements NamingStrategyInterface
7-
{
8-
eagerJoinRelationAlias(alias: string, propertyPath: string): string {
9-
return alias + "__" + propertyPath.replace(".", "_")
10-
}
11-
}
6+
implements NamingStrategyInterface {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src"
2+
3+
import { B } from "./B"
4+
5+
@Entity()
6+
export class A {
7+
@PrimaryGeneratedColumn("increment")
8+
id!: number
9+
10+
@Column(() => B)
11+
b!: B
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { OneToMany } from "../../../../src"
2+
import { C } from "./C"
3+
4+
export class B {
5+
@OneToMany(() => C, (c: C) => c.a, {
6+
cascade: true,
7+
eager: true,
8+
})
9+
cs!: C[]
10+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {
2+
Entity,
3+
ManyToOne,
4+
OneToMany,
5+
PrimaryGeneratedColumn,
6+
} from "../../../../src"
7+
import { A } from "./A"
8+
import { D } from "./D"
9+
10+
@Entity()
11+
export class C {
12+
@PrimaryGeneratedColumn("increment")
13+
id!: number
14+
15+
@ManyToOne(() => A, (a) => a.b.cs, {
16+
onDelete: "CASCADE",
17+
onUpdate: "CASCADE",
18+
nullable: false,
19+
})
20+
a!: A
21+
22+
@OneToMany(() => D, (d) => d.c, {
23+
cascade: true,
24+
orphanedRowAction: "delete",
25+
eager: true,
26+
})
27+
ds!: D[]
28+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Entity, ManyToOne, PrimaryGeneratedColumn } from "../../../../src"
2+
import { C } from "./C"
3+
4+
@Entity()
5+
export class D {
6+
@PrimaryGeneratedColumn("increment")
7+
id!: number
8+
9+
@ManyToOne(() => C, (c) => c.ds, {
10+
onDelete: "CASCADE",
11+
onUpdate: "CASCADE",
12+
})
13+
c!: C
14+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import "reflect-metadata"
2+
import {
3+
createTestingConnections,
4+
closeTestingConnections,
5+
reloadTestingDatabases,
6+
} from "../../utils/test-utils"
7+
import { DataSource } from "../../../src/data-source/DataSource"
8+
import { expect } from "chai"
9+
import { A } from "./entity/A"
10+
import { B } from "./entity/B"
11+
import { C } from "./entity/C"
12+
13+
describe("github issues > #9944 Alias Issue With Nested Entity And Relations", () => {
14+
let dataSources: DataSource[]
15+
before(
16+
async () =>
17+
(dataSources = await createTestingConnections({
18+
entities: [__dirname + "/entity/*{.js,.ts}"],
19+
schemaCreate: true,
20+
dropSchema: true,
21+
relationLoadStrategy: "query",
22+
enabledDrivers: ["mysql", "postgres"],
23+
})),
24+
)
25+
beforeEach(() => reloadTestingDatabases(dataSources))
26+
after(() => closeTestingConnections(dataSources))
27+
28+
it("Validate correct loading of eager, nested relations", () =>
29+
Promise.all(
30+
dataSources.map(async (dataSource) => {
31+
const aEntity = new A()
32+
aEntity.b = new B()
33+
aEntity.b.cs = [new C()]
34+
await dataSource.manager.save(aEntity)
35+
36+
const [fetchedA] = await dataSource.manager.find(A)
37+
expect(fetchedA.b).exist // Validates correct relation A.b is loaded
38+
expect(fetchedA.b.cs).exist // Validates correct relation A.b.cs is loaded
39+
expect(fetchedA.b.cs[0]).exist
40+
}),
41+
))
42+
})

0 commit comments

Comments
 (0)