BUG: `Having` doesn't work with function as column
See original GitHub issueSee this runkit for a quick example: https://runkit.com/friend0/59e6fa9e8caff40012a0c1cd
var knex = require("knex")({client: 'pg'})
const query = knex.select().from('A').groupBy('count').having(function (){
return this.having('1', '=', 'null')
})
console.log(query.toString());
const query1 = knex.select().from('A').groupBy('count').where(function (){
return this.where('1', '=', 'null')
})
console.log(query1.toString());
Produces:
>>> "select * from \"A\" group by \"count\""
>>> "select * from \"A\" where (\"1\" = 'null') group by \"count\""
I expected:
>>> "select * from \"A\" group by \"count\"" having (\"1\" = 'null')
>>> "select * from \"A\" where (\"1\" = 'null') group by \"count\""
(the queries are non-sense, of course)
where
accepts a function as parameter for composing complex sequences.
having
, it appears, also supports this (poking through the source, doesn’t seem to be documented) but doesn’t produce the query I’d expect. Am I doing something wrong, or is this a bug I should look into?
This issue appear to be long-lived, but wasn’t tagged well. https://github.com/tgriesser/knex/issues/1771
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
"Unknown column 'foo' in 'having clause'" when using function ...
If the GROUP BY clause keeps using the function, but the HAVING clause does not, the HAVING clause works. If the SELECT clause...
Read more >MySQL : Having doesn't work - Stack Overflow
I am using MySQL. Here is my schema: Table b. Column Name | ...
Read more >Excel VLOOKUP not working - fixing #N/A and #VALUE errors
Is your VLOOKUP pulling wrong data or you cannot get it to work at all? This tutorial shows how you can quickly fix...
Read more >How to correct a #N/A error in the VLOOKUP function
Problem : The lookup value is not in the first column in the table_array argument. One constraint of VLOOKUP is that it can...
Read more >Frequently Asked Questions - SQLite
What is an SQLITE_SCHEMA error, and why am I getting one? I get some compiler warnings when I compile SQLite. Isn't this a...
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
@aldeed Huh, it indeed looks like an intended behavior 😮 I think it would be just easiest and safest to document current functionality of nested having for now and not touch it.
I don’t see much benefit on changing it to
'having'
.@friend0 @elhigu I just ran into this issue. I was going to attempt a PR when I realized that it does work, except that you have to use
where
within thehaving
callback. There is a test for it here, where you can clearly see the weirdness: https://github.com/tgriesser/knex/blob/master/test/unit/query/builder.js#L1393-L1404I have to imagine that was an accident? If so, there is an easy fix. Change
'where'
to'having'
here: https://github.com/tgriesser/knex/blob/master/src/query/compiler.js#L325I would make this change but I’m not sure if you want to since people might be relying on the
where
behavior now. Could be considered a breaking change.