-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Closed
Labels
Description
Issue description
When have multiple relations with same column, causes unspected Errors
Expected Behavior
dataSource.getRepository(EstateModel).find({
relations: ['city', 'pref']
});
should result in this relational left join sql :
LEFT JOIN `E_PREF` `EstateModel__EstateModel_pref` ON
`EstateModel__EstateModel_pref`.`PREF_ID` = `EstateModel`.`PREF_ID`
LEFT JOIN `M_CITY` `EstateModel__EstateModel_city` ON
`EstateModel__EstateModel_city`.`CITY_ID` = `EstateModel`.`PREF_ID` AND
`EstateModel__EstateModel_city`.`PREF_ID` = `EstateModel`.`PREF_ID`
Actual Behavior
LEFT JOIN `E_PREF` `EstateModel__EstateModel_pref` ON
`EstateModel__EstateModel_pref`.`PREF_ID` = `EstateModel`.`PREF_ID`
LEFT JOIN `M_CITY` `EstateModel__EstateModel_city` ON
`EstateModel__EstateModel_city`.`CITY_ID` = `EstateModel`.`PREF_ID` AND
`EstateModel__EstateModel_city`.`CITY_ID` = `EstateModel`.`PREF_ID`
Steps to reproduce
@Entity({name: "M_ESTATE"})
export default class EstateModel {
@PrimaryGeneratedColumn({name: "ESTATE_ID"})
id!: number;
@Column({name: "ESTATE_NAME", type: "varchar", length: 100, nullable: false})
name!: string;
@Column({ name: 'PREF_ID', comment: '都道府県ID' })
prefId!: string;
@Column({ name: 'CITY_ID', comment: '市区ID' })
cityId!: string | null;
@OneToOne(() => CityModel)
@JoinColumn([
{ name: 'CITY_ID', referencedColumnName: 'id', },
{ name: 'PREF_ID', referencedColumnName: 'prefId'}
])
city!: CityModel | null;
@OneToOne(() => PrefModel)
@JoinColumn([
{ name: 'PREF_ID', referencedColumnName: 'id'}
])
pref!: PrefModel | null;
}
@Entity({name: "E_PREF" })
export default class PrefModel {
@PrimaryGeneratedColumn({name: 'PREF_ID'})
id!: string;
@Column({name: 'PREF_NAME', nullable: false})
name!: string;
@Column({name: 'PREF_KANA', nullable: false})
kanaName!: string;
}
@Entity({name: "M_CITY" })
export default class CityModel {
@PrimaryColumn({ name: 'CITY_ID', nullable: false })
id!: string;
@PrimaryColumn({ name: 'PREF_ID', nullable: false })
prefId!: string;
@Column({ name: 'CITY_NAME', nullable: false })
name!: string;
}
dataSource.getRepository(EstateModel).find({
relations: ['city', 'pref']
});
My Environment
| Dependency | Version |
|---|---|
| Operating System | Windowns/Linux |
| Node.js version | 16.14.0 |
| Typescript version | 4.9.5 |
| TypeORM version | 0.3.16 |
Additional Context
Note here, City model have two primary columns, so the relation between estate and pref need these two columns to work, and a Estate can have a Pref and does not have a City, so i need these two relation in this model
Relevant Database Driver(s)
- aurora-mysql
- aurora-postgres
- better-sqlite3
- cockroachdb
- cordova
- expo
- mongodb
- mysql
- nativescript
- oracle
- postgres
- react-native
- sap
- spanner
- sqlite
- sqlite-abstract
- sqljs
- sqlserver
Are you willing to resolve this issue by submitting a Pull Request?
Yes, I have the time, but I don't know how to start. I would need guidance.
EternalDeiwos