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.

postgresql `orderBy()` does not properly utilize NULLS third argument

See original GitHub issue

Environment

Knex version: 0.95.14 Database + version: postgres 14.0

Bug

  1. The orderBy() command in postgres does not function properly for postgres. Currently, the command orderBy('field', 'desc', 'first') compiles to ORDER BY ("field" is NULL) desc but in postgres it needs to be ORDER BY "field" desc NULLS LAST.

  2. Example generating incorrect order strings - https://runkit.com/embed/k2t79bssiic7

  3. Documentation for the proper postgres syntax is at https://www.postgresql.org/docs/14/queries-order.html .

I’d be happy to attempt to fix the problem if someone could point me to the right files in the code that modified this behavior. I looked around the code but couldn’t find an obvious place in the https://github.com/knex/knex/blob/master/lib/dialects/postgres/query/pg-querycompiler.js where it would go.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
OlivierCavadenticommented, Nov 23, 2021

So if I add a function with the exact same name into the postgres-querycompiler I can expect the same input/output contract and would just need to make it spit out the postgres equivalent?

Yes, if you put the function with the same name, like in mssql-querycompiler, knex will use it.

For the unit tests where do I go to add the unit tests and bootstrap in the test data accordingly? Is there an good example in the repo I can use as a blueprint?

Please see the test with name ‘order by with null’ in selects.spec.js (line 562). I made an integration test but if you have the bug, maybe it’s not good enough.

For unit test, see in builder.js (in test/unit/query), line 5748 (first test is ‘order by, null first’).

0reactions
kibertoadcommented, Feb 1, 2022

Released in 1.0.2

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation: 15: 7.5. Sorting Rows (ORDER BY)
The NULLS FIRST and NULLS LAST options can be used to determine whether nulls appear before or after non-null values in the sort...
Read more >
Documentation: 15: 43.5. Basic Statements
If STRICT is not specified in the INTO clause, then target will be set to the first row returned by the command, or...
Read more >
Documentation: 15: 38.5. Query Language (SQL) Functions
The simplest possible SQL function has no arguments and simply returns a base type, such as integer : CREATE FUNCTION one() RETURNS integer...
Read more >
15: 9.2. Comparison Functions and Operators
A null input is treated as the logical value “unknown”. Notice that IS UNKNOWN and IS NOT UNKNOWN are effectively the same as...
Read more >
Documentation: 15: SELECT
[ NULLS { FIRST | LAST } ] [, ...] The ORDER BY clause causes the result rows to be sorted according to...
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