typeorm
typeorm copied to clipboard
`leftJoinAndMapOne` doesn't add property when custom query builder is used
Issue Description
Coming from #1882. Running a query builder in leftJoinAndMapOne
doesn't map to property.
Expected Behavior
leftJoinAndMapOne
should map to the provided property.
Actual Behavior
I am trying to do something like
const [venues, venuesCount] = this.venuesRepository
.createQueryBuilder("Venue")
.leftJoinAndSelect("Venue.city", "city")
.leftJoinAndMapOne(
"Venue.eventStats",
(qb) =>
qb
.from("events", "Event")
.select('"venueId"')
.addSelect(`MAX("capacity")`, "maxVenueCapacity")
.addSelect(`COUNT("venueId")`, "eventsCount")
.groupBy('"venueId"'),
"Event",
"Venue.id = Event.venueId"
)
.where(`Venue.cityId = :cityId`, { cityId: "someCityId" })
.orderBy(`eventCount`, `DESC`)
.getManyAndCount();
And my model has
interface EventStats {
maxVenueCapacity: number;
eventsCount: number;
}
@Entity()
class Venue {
eventStats: EventStats;
}
at the end venues[].eventStats
is not added.
// include the output in code tags like these!
Steps to Reproduce
- Use the code I provided above to create a similar query.
- Run the query and check if the mapped property is getting added.
My Environment
Dependency | Version |
---|---|
Operating System | Docker Node Alpine Image |
Node.js version | 14.17.0 |
Typescript version | 4.5.4 |
TypeORM version | 0.2.41 |
Additional Context
Relevant Database Driver(s)
DB Type | Reproducible |
---|---|
postgres |
yes |
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.
is still a issue
From what I can see the issue is that unless you use an Entity class or a relation property, typeorm cannot know what the mappings are from the properties on the custom query.
One solution seems fairly simple - have an extra argument on this overload for the user to supply the correct Entity class constructor to use for the mapping. Though this is so simple I'm wondering if there's a reason this hasn't already been done. From what I can see this issue has been present for a long time.
@prateekkathal does that help you in anyway to put an MR together for it?
@prateekkathal I've created an MR to fix this - although the user has to pass the entity for the mapping as it might not always be able to get this from the joined query builder.
@prateekkathal I've created an MR to fix this - although the user has to pass the entity for the mapping as it might not always be able to get this from the joined query builder.
Thanks for creating the PR. I leave it up to the mods/admins to approve the solution. Personally I feel that defining the property inside the Entity itself should automatically map it. I think we should understand the reasoning as to why it doesn't do it now when it already works for other entities rather than just adding another parameter. But if the admins agree with your solution, nothing like it 😄
Once again, thanks for taking the lead here. I missed your last message and never personally got the chance to contribute. Glad that you could!
Looking forward to a solution getting merged! 👍