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.

BUG: `Having` doesn't work with function as column

See original GitHub issue

See 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:open
  • Created 6 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
elhigucommented, Nov 30, 2017

@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'.

0reactions
aldeedcommented, Nov 29, 2017

@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 the having 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-L1404

I 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#L325

I 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.

Read more comments on GitHub >

github_iconTop 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 >

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