-
Notifications
You must be signed in to change notification settings - Fork 122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrong totalCount when using joins in QB #627
Comments
I can provide failing test, but the issue is pretty obvious. I'm curious what was the motivation for not using TypeORMs |
sure, I haven't looked into this yet, don't suppose you'd know a way in typeorm to get the primary key column? |
well in TypeORM they are able to get the primary columns from the metadata https://github.com/typeorm/typeorm/blob/dd94c9d38d62c98f7afd5396abbc7e32ba689aad/src/query-builder/SelectQueryBuilder.ts#L1833 |
Yea we need a more advanced query which I was trying to implement for others which doesn't have the distinct func so adding that will solve your problem and enable more advanced queries for others! |
@bashleigh I've added the failing test as promised. Hope it's helpful. |
Haven't forgotten, been pretty ill. Don't suppose you could make a PR for your changes? That way I might as well merge them into master and solve the problem there |
is there any chance to fix it? |
I ran into the same problem. |
Honestly, I haven't had a lot of time to look into this unfortunately. Please feel free to have a go yourselves! Really sorry I've left it so long! |
@rudolfmedula @NikolayYakovenko use the below code snippet, I hope this will resolve your problem, this helped me with the same issue I had faced in my application |
@ayeen, thank you for your advice. I know about this option and used it. |
@NikolayYakovenko this means you are missing something like you are query on the wrong table which shows the total of 10 items and in a result that shows 5 items from join table. |
Same problem :( |
@FelipeGerolomo I face this issue when I am trying to order by the data from inner relation! I found one solution I set the order by entity definition and remove it from the query then its worked fine for me. |
Same problem with NikolayYakovenko :( |
Same issue here. |
Not only is the total count wrong with the most recent E.g.
Gives me 1 vendor with 5 features instead of 5 vendors with 5 features each. Even though I only have 10 vendors with 5 features each, the meta gives me:
@ayeen's solution fixes the items but the totalItems in the meta block remain wrong. |
made a quick release today, I'll give it another go today (got a few hours spare currently 🤞🏼) what I need to do to solve this once and forall would be to add a groupBy on the parent table with the primary key however not everyone's setup is the same so this would break in some cases. Plus I would need to resolve this myself without adding a new property config (because who wants to specify their primary key) which shouldn't be an issue. It's the fact that not everyone will have a primary key that's making me feel it's not a good enough solution. Have also noticed this function |
Same issue in my case :( |
any news about this ? I have same issue with 3.2.0 |
we stays on 2.x version because of this bug |
My solution with this problem, was return the function async listAll({
page,
limit,
}: IListAllProps): Promise<Pagination<PeopleM>> {
const peoples = this.peopleEntityRepository
.createQueryBuilder('p')
.leftJoinAndSelect('p.lead_step', 'lead')
.leftJoinAndSelect('p.schedules', 's')
.select(['p', 'lead', 's.id', 's.started_at', 's.finished_at']);
// Here I get total items of table peoples
const totalItems = await peoples.getCount();
return await paginate<People>(peoples, {
limit,
page,
paginationType: PaginationTypeEnum.TAKE_AND_SKIP,
metaTransformer: ({ currentPage, itemCount, itemsPerPage }) => {
// Calculating the total of pages
const totalPages = Math.round(totalItems / itemsPerPage);
return {
currentPage,
itemCount,
itemsPerPage,
// Returning in this two row
totalItems,
totalPages: totalPages === 0 ? 1 : totalPages,
};
},
});
} I know it’s not right this way, but was that I found at moment. |
|
Thank you, that's right! |
what did you change to resolve it? Still seems to be an issues for me. |
I'm also facing the issue, any workarounds? |
Any news about this ? |
For now the only workaround we've found is to remove the join from the query, but that wouldn't work everytime. has this fallen off the radar ? |
Inspired by the other comments, my current workaround is:
|
@bashleigh, if your concern is about the presence of primary keys that getCount() seems to rely on, here is a proposal for a workaround (not tested).
|
Hi team, any update on this issue? I am having a query with join where I limit is 20, but the actual record only returns 10. |
This fixes the problem, but It stops returning the "links" property for the next, previous page etc. Is there a way to keep the links ? @psam44 |
It stops returning the "links" property because it needs que countQuery information to build the property and countQueries is set to false since it does the count wrong . For anyone that gets here , the logic of how the links property is build is on the createPaginationObject :
The function is exported so you can use it with the correct totalItems count |
@andrescjhl Right, my project doesn't use the |
@psam44 there's a few issues really. The difficult part about this problem is a "one shoe fits all" solution for everyone's individual needs with different queries. Yes the |
In this commit 890ee16#diff-c58e144dadf00250381ed55e6ce83245cda76aca84131ad494fd4911932f656f. a custom total count query was introduced instead of TypeORMs
qb.getCount()
;this query being SELECT COUNT(*) FROM (..) returns number of all rows returned by query which would be like (m*n* k...) depending on how many rows were joined
the original implementation of TypeORMs getCount does
SELECT COUNT(DISTINCT("table"."id")) AS "cnt" FROM (..)
which returns correct number of entities even when using JOINs
The text was updated successfully, but these errors were encountered: