findAndCountAll with virtual columns and "having", drops virtual column in the count query.
See original GitHub issueFollowing up on https://github.com/sequelize/sequelize/issues/9739, which you closed. It’s still an issue with latest sequelize 4.38
Using MySQL 5.7 Sequelize 4.38
Same error, still valid as above:
let options = {
attributes: [
'id',
[sequelize.literal( `select 1` ), `has_access`]
],
having: {'$has_access$': 1},
}
let result = await myTable.findAndCountAll(options);
gives error:
{
"error": "error",
"err": {
"name": "SequelizeDatabaseError",
"parent": {
"code": "ER_BAD_FIELD_ERROR",
"errno": 1054,
"sqlState": "42S22",
"sqlMessage": "Unknown column 'has_access' in 'having clause'",
"sql": "SELECT count(*) AS `count`
FROM `mytable` AS `mytable` HAVING `has_access` = 1;"
},
"original": {
"code": "ER_BAD_FIELD_ERROR",
"errno": 1054,
"sqlState": "42S22",
"sqlMessage": "Unknown column 'has_access' in 'having clause'",
"sql": "SELECT count(*) AS `count`
FROM `mytable` AS `mytable` HAVING `has_access` = 1;"
},
"sql": "SELECT count(*) AS `count`
FROM `mytable` AS `mytable` HAVING `has_access` = 1;"
},
"err_msg": "Unknown column 'has_access' in 'having clause'",
"err_stack": "SequelizeDatabaseError: Unknown column 'has_access' in 'having clause'\n at
And now, I get the same error with this alternate query:
let options = {
attributes: [
'id',
[sequelize.literal( `select 1` ), `has_access`]
],
having: {'$has_access$': 1},
}
let [countResult,findResult] = await Promise.all([ mytable.count(options), mytable.findAll(options) ]);
let result = {count: countResult, rows: findResult};
If findAndCountAll
has options to enable a virtual column, it isn’t clear from the documentation for findAndCountAll nor in the documentation for count
Seems like a bug… or unsupported feature of using subqueries. Any ideas?
Have you considered using SQL_CALC_FOUND_ROWS and SELECT FOUND_ROWS() as an alternate way to count? Is there a way to use this MySQL feature? Perhaps I can generate a raw MySQL query with findAll(), and wrap with SQL_CALC_FOUND_ROWS and SELECT FOUND_ROWS()…
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:23 (5 by maintainers)
Top Results From Across the Web
How to avoid expensive virtual column expressions
The virtual columns for table 1 are computed right after table 1 is fetched. But let's say one of them requires table 8....
Read more >How can I add a virtual "count" column to a select query in rails?
I am looking for a way that I can fetch all posts, and have a virtual attribute on the record which will display...
Read more >unknown column in 'where clause sequelize nestjs - You.com
Sequelize include nested column: Unknown column in 'where clause' ... with virtual columns and "having", drops virtual column in the count query.#9747.
Read more >Model - Sequelize
async findAndCountAll(options: object): Promise<{count: number|number[], ... returns all values of the instance, also invoking virtual getters.
Read more >mongodb count data in collection Code Example - Code Grepper
Queries related to “mongodb count data in collection” · count mongodb · count documents mongodb · mongodb find count · mongo db count...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Can this be re-opened? This is still an issue and means we can’t use
findAllAndCount
when we’re also using a virtual field/aggregator on the attributes.Here is some code that implements a full count needed for pagination, and supports virtual (calculated) columns with
having
as shown above:outputs:
thank you to: https://github.com/sequelize/sequelize/issues/2325#issuecomment-366060303