-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Description
I just encountered some weird behaviour when trying to use @JoinColumn multiple times for the same column.
I've created a simple example to show the behaviour and make it reproducable:
@Entity()
export class TypeormTestPrimary {
@PrimaryGeneratedColumn({
type: 'int',
})
ID: number;
@Column({
type: 'int',
})
SecondaryID: number;
@ManyToOne(() => TypeormTestSecondary)
@JoinColumn({ name: 'SecondaryID', referencedColumnName: 'ID' })
secondary: TypeormTestSecondary;
@ManyToOne(() => TypeormTestTertiary)
@JoinColumn({ name: 'SecondaryID', referencedColumnName: 'UserID' })
tertiary: TypeormTestTertiary;
}
@Entity()
export class TypeormTestSecondary {
@PrimaryGeneratedColumn({
type: 'int',
})
ID: number;
}
@Entity()
export class TypeormTestTertiary {
@PrimaryColumn({
type: 'int',
})
SecondaryID: number;
@PrimaryColumn({
type: 'int',
})
UserID: number;
}When trying to load the first entity TypeormTestPrimary with the secondary and tertiary relations, I'm getting the following error: Unknown column 'TypeormTestPrimary__TypeormTestPrimary_secondary.UserID' in 'on clause'.
The error shows that TypeORM tries to read the column UserID (which was used as the referenced column for the tertiary relation) on the TypeormTestSecondary entity, where the column obviously doesn't exist.
If I reverse the order of the relations it tries to do the same the other way around: It tries to read the column ID on the TypeormTestTertiary entity.