typeorm: leftJoinAndMapOne does not add target property to entity
Issue type:
[ x] question [ ] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[x] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)
The above will return the User, but without the lastPurchase property
const users = await this.repository.createQueryBuilder("user")
.leftJoinAndMapOne("user.lastPurchase", (subQuery) => {
return subQuery
.select()
.from(Purchase, "purchase")
.orderBy("created", "DESC");
}, "purchase", "purchase.userId = user.id")
.getManyAndCount();
any ideas?
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 12
- Comments: 19 (6 by maintainers)
Commits related to this issue
- fix: use subquery with join map one methods Closes: #5637 — committed to xdc0/typeorm by xdc0 4 months ago
- fix: use subquery with join map one methods Closes: #5637 — committed to xdc0/typeorm by xdc0 4 months ago
@FFsim
BTW, to achieve join and map last rows like following code…
You can use it.
I’ve posted a potential solution for this: https://github.com/typeorm/typeorm/pull/9354
Found a workaround, let’s assume that you have join like this
So you need to get raw results and mapped entities via
.getRawAndEntities()and map custom properties manually:The solution is not clean, you still can’t combine it with count query, but this is the less painful way to achieve a result
Don’t forget to add your custom properties in
@Entity()Fixed on: https://github.com/typeorm/typeorm/pull/10769
Same here. Using the same query, results from
.getRawMany()include the mapped field but those from.getMany()don’tLuckily, Contract table has OneToMany relationship to Lesson table, so the following query
gets me roughly what I want with the first lesson inside
contract.lessonsarrayStill I would highly appreciate if someone can address this issue or tell me what I did wrong in the first query. Thanks!
@imnotjames due to project deadline I have fixed this other way using raw query.