question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unknown generated column in 'having clause' when findAndCountAll

See original GitHub issue

I am trying to get back rows and count with specific

            models.Billboard.findAndCountAll({
                attributes: [
                    'Id',
                    'status',
                    ['( 6371 * acos( cos( radians('+location[0]+') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('+location[1]+') ) + sin( radians('+location[0]+') ) * sin( radians( lat ) ) ) )', 'distance'],
                ],
                having: {
                    distance: {
                        $lt: 5
                    }
                },
                order: ['id'],
                limit: 15,
                offset: page - 1
            })
            .then( function(result) {

                // cache save
                cache.set( hash, result, function(err, success) {

                    res.setHeader('Content-Type', 'application/json');
                    res.send(JSON.stringify(result, null, 3));
                    res.end();
                } );

            });

Query executed by Sequelize in findAll:

SELECT `Id`, `status`, ( 6371 * acos( cos( radians(20.6596988) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-103.34960920000003) ) + sin( radians(20.6596988) ) * sin( radians( lat ) ) ) ) AS `distance` FROM `Billboards` AS `Billboard` HAVING `distance` < 5 ORDER BY `id` LIMIT 15;

Query executed by Sequelize in findAndCountAll:

SELECT count(*) AS `count` FROM `Billboards` AS `Billboard` HAVING `distance` < 5;

Result:

Unhandled rejection SequelizeDatabaseError: ER_BAD_FIELD_ERROR: Unknown column ‘distance’ in ‘having clause’

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
hardikphpcommented, Feb 6, 2018

@jamesone did you get any solutions ? I am also facing same issue

1reaction
jamesonecommented, Apr 15, 2016

I was running a similar query (making calculation in attribute, named it distance), the only way to reference that value in my having: clause was to put single quotes around it (probably works with double aswell), I’m pretty sure Sequelize tried to look for the column distance in my model and then just defaulted to ‘distance in my query’

Sent from Outlook

On Fri, Apr 15, 2016 at 4:02 AM -0700, “Gustavo Straube” notifications@github.com wrote:

@jamesone By brackets ({ and }) you mean single quotes or apostrophes (')? If so, maybe I’m wrong, but I don’t think that make any difference in the runtime. Both snippets below, although the slightly different syntax, give the same result:

‘distance’: { $lt: 5 }

and

distance: { $lt: 5 }

However, if you’re definitely talking about the brackets, @jjacquesf used in the exact same syntax you mentioned – refer to the first comment. And it didn’t work.

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unknown generated column in 'having clause' when ...
I am trying to get back rows and count with specific models.Billboard.findAndCountAll({ attributes: [ 'Id', 'status', ['( 6371 * acos( cos( ...
Read more >
"Unknown Column" error in Sequelize when using "limit" and ...
The error is accurate as the WHERE clause that's generated in the sub-query is referencing a column not included in the sub-query.
Read more >
Unknown column in Having Clause - DBA Stack Exchange
Although the objective has been achieved by using Where Clause before Group By, but this error is still bothering. Any help would be...
Read more >
sequelize query use column name instead model name
Step 3: Open newly created migration file and add below code ... Error SequelizeDatabaseError: Unknown column 'A.b_id' in 'order clause' is rising.
Read more >
"Unknown column 'foo' in 'having clause'" when using function ...
Bug #103961, Error: "Unknown column 'foo' in 'having clause'" when using function in HAVING. Submitted: 9 Jun 2021 14:01, Modified: 26 Jun 2021...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found