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
Sequelize findAndCountAll returns wrong count when association models included #9481
Comments
@tumdav try adding distinct:true to the findAndCountAll passed object that should do the trick, i had the same problem few days ago and that solve it
|
I've the same issue adding |
@tumdav - I'm happy to take a look at this but can you provide a complete set of your models your using for this example please. Seems that your missing users above. |
Give this workaround a try: // Add a permanent global hook to prevent unknowingly hitting this Sequelize bug:
// https://github.com/sequelize/sequelize/issues/10557
sequelize.addHook('beforeCount', function (options) {
if (this._scope.include && this._scope.include.length > 0) {
options.distinct = true
options.col = this._scope.col || options.col || `"${this.options.name.singular}".id`
}
if (options.include && options.include.length > 0) {
options.include = null
}
}) |
@JamesMGreene I've had this problem for a long time and this worked beautifully (even though I don't quite understand how...). So thanks a million !! 😄 |
The bug is introduced in this PR to fix another issue. Currently the The previous version of this issue was fixed and closed a while ago. |
this bug is apparently due to sequelize findAndCountAll method which adds extra count in cases where there are other models included (in this case Likes model is included in the getMessages result). Adding distinct: true appears to fix the bug. see sequelize/sequelize#9481
I added this hook inside models/index.js and it works 👍
|
I've run into this issue numerous times, and today encountered it again. 3+ years and it hasn't been fixed? There is obviously a workaround available posted by @JamesMGreene , thanks for that, but it shouldn't be required for such a core piece of functionality of Sequelize to work correctly. This example is even in the official docs and it doesn't even work! |
The complex your queries get, the workaround stops working. For example, If I want to pass const data = User.findAndCountAll({ include: [{as: 'order', model: Order, where: { orderName: 'laptop' } }] }) Now the count will be wrong with the workaround by @JamesMGreene (which was genius, tho). This needs to be properly fixed |
the issue seems to be fixed, adding |
At first I thought this was a bug but it appears it's just critical to read the docs. When using findAndCountAll(), make sure to pass the same params as you would to a call to count(). Namely: 'col' and 'distinct'. I think most users here will want to use the root object id as the 'col' value, and 'distinct' set to true. |
|
Thank you! distinct: true was the answer I was looking for. |
Try to used separate: true in association if you count is wrong when you used findAndCountAll with associate |
Adding
Worked for me with this issue in Sequelize v6.19.2. Granted it runs another query for the associations, but the counts seem to be right (at least for my query use-case). Good luck. EDIT: Ran into a weird bug where adding |
added beforeCount hook inside the DB connection file this snippet will fix the wrong count issue in findAndCountAll()
|
working for mysql |
thank you bro |
Sequelize is performing two queries for findAndCountAll—one for performing the original query and another for counting the records.
Using distinct=true solves the problem in some cases. But the problem remains the same in another case and causes the wrong number to be returned as a count. I came up with a solution by creating my method. Using nest js and sequelize-typescript:
|
What are you doing?
I have Post model.
Post model has attachment(oneToMany),users(oneToMany),
I'm trying to fetch posts with attachments using sequelize
findAndCountAll
POST TABLE
POST ATTACHMENT TABLE
SEQUELIZE QUERY
What do you expect to happen?
I'm expecting get 2 posts and total sum
2
What is actually happening?
I'm getting 2 posts and total sum
4
Dialect: postgres
Dialect version: XXX
Database version: PostgreSQL 10.3 on x86_64-apple-darwin17.3.0, compiled by Apple LLVM version 9.0.0 (clang-900.0.39.2), 64-bit
Sequelize version: ^4.37.8
The text was updated successfully, but these errors were encountered: