JoinColumn takes only first foreign key into account when passing an array as parameter
See original GitHub issueIssue type:
[ ] question [x] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[x] mysql
/ mariadb
[ ] oracle
[ ] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[ ] latest
[ ] @next
[x] 0.2.7
Steps to reproduce or a small repository showing the problem:
Let’s imagine situation that Photo row in database has two foreign keys which reference to Author row in database. In that case we want to do query where we left join on both foreign keys. Below you can see the models where I pass array of foreign keys to JoinColumn
. Also according to the documentation JoinColumn
should accept arrays.
// Author.ts
import { OneToMany } from 'typeorm';
export class Author {
@OneToMany(type => Photo, photo => photo.author)
photos!: Photo[];
}
// Photo.ts
import { ManyToOne, JoinColumn } from 'typeorm';
export class Photo {
@ManyToOne(type => Author, author => author.photos)
@JoinColumn([{ name: 'from_id' }, { name: 'to_id' }])
author!: Author;
}
But when I do query like this:
this.authorRepository
.createQueryBuilder('author')
.leftJoinAndSelect('author.photos', 'photo')
.where('author.id = :id', { id: 123 })
.orderBy({
'author.last_photo_time': 'DESC',
'photo.created': 'DESC',
})
.printSql()
.getMany();
it produces wrong query
SELECT
`author`.`id` AS `author_id`,
`author`.`created` AS `author_created`,
`author`.`updated` AS `author_updated`,
`author`.`last_photo_time` AS `author_last_photo_time`,
`author`.`last_photo` AS `author_last_photo`,
`author`.`last_photo_from_id` AS `author_last_photo_from_id`,
`photo`.`mid` AS `photo_mid`,
`photo`.`from_id` AS `photo_from_id`,
`photo`.`to_id` AS `photo_to_id`,
`photo`.`from_id` AS `photo_from_id`,
`photo`.`to_id` AS `photo_to_id`,
`photo`.`photo` AS `photo_photo`,
`photo`.`created` AS `photo_created`
FROM
`author` `author`
LEFT JOIN
`photo` `photo` ON `photo`.`from_id` = `author`.`id` // here should be also `photo`.`to_id` = `author`.`id`
WHERE
`author`.`id` = 123
ORDER BY `author`.`last_photo_time` DESC , `photo`.`created` DESC
As you can see from the query LEFT JOIN
is missing the second join on the to_id
foreign key.
So looks like JoinColumn
take into account only the first object in the array because if I put to_id
as first array item then to_id
was in the produces query.
So the here is wrong behaviour:
LEFT JOIN photo ON photo.from_id = author.id
Here would be correct behaviour when this bug is fixed:
LEFT JOIN photo ON photo.from_id = author.id OR photo.to_id = author.id
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
I don’t know the codebase at all so I have created here entity definition which can be reused.
Basically
db_photo
has TWO foreign keysfrom_cid
andto_cid
which point to the primary keycid
ondb_author
.And when creating query like this
I get this query:
But instead I though it would be like this because I passed array to
JoinColumn
Is this more helpful for you @Kononnable ? 😃
We just did custom query because this is such a edge case