-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Description
It seems that the resolution of relation joins seems to get messed up if the source name is used in more than one relation in one entity.
@Entity('EXAMPLE')
export class Example {
@Column('varchar', {name: 'EXA_A'})
a!: string;
@Column('varchar', {name: 'EXA_B'})
b!: string;
// 1
@ManyToOne(() => cEntity, (centity: cEntity) => centity.a)
@JoinColumn([{ name: 'EXA_A', referencedColumnName: 'aNr' }])
aToC!: cEntity;
// 2
@ManyToOne(() => dEntity, (dentity: dEntity) => dentity.b)
@JoinColumn([
{ name: 'EXA_A', referencedColumnName: 'aToBNr' },
{ name: 'EXA_B', referencedColumnName: 'bNr' },
])
bToD!: dEntity;
}This seems to rather easy disregarding what those other entities might look like. But the result is different depending on the order of those two relations
-- 1 first
SELECT
"Example"."EXA_A" AS "Example_EXA_A",
"Example"."EXA_B" AS "Example_EXA_B"
FROM
"EXAMPLE" "Example"
LEFT JOIN "CENTITY" "Example__Example_aToC"
ON "Example"."EXA_A" ="Example__Example_aToC"."DEN_ATOBNR"-- 2 first
SELECT
"Example"."EXA_A" AS "Example_EXA_A",
"Example"."EXA_B" AS "Example_EXA_B"
FROM
"EXAMPLE" "Example"
LEFT JOIN "DENTITY" "Example__Example_bToD"
ON "Example"."EXA_A" ="Example__Example_bToD"."CEN_ANR"
AND "Example"."EXA_B" ="Example__Example_bToD"."DEN_BNR"so the part with the same name in the join column overwrites all other depending on the order they are defined in the entity.
This can be seen in below code at runtime because joinColumn.referencedColumn!.propertyPath will not match with the relation of the entity definition for the query.
This also includes cases where you have ... .find({ relations: { dEntity: true }}) so only one relation in a find the other will still overwrite it depending on the order.
https://github.com/typeorm/typeorm/blob/master/src/query-builder/SelectQueryBuilder.ts#L2280