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.

.executeTakeFirstOrThrow() returns null instead of throwing

See original GitHub issue

Description

When executing a SELECT query with .executeTakeFirstOrThrow(), it returns null if the result set is empty instead of throwing an exception.

Environment

Name Version
macOS 13.0
Node 16.17.0
npm 8.15.0
Kysely 0.22.0
Kysely Codegen 0.7.4
MySQL 5.7.38
mysql2 2.3.3

Additional Details

  • Query was verified to return [] with .execute()
  • Query was verified to return undefined with .executeTakeFirst()
  • Query was verified to throw a NoResultError exception when the connection details were invalid
  • Return type from .executeTakeFirstOrThrow() does not reflect the potential for null
  • Issue occurs when using min and max aggregate functions; potentially others
  • Query verified to throw a NoResultError when selecting columns without using aggregate functions

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
koskimascommented, Oct 25, 2022

Yep, that query always returns one row, even if the set from which max is taken is empty. That’s how SQL works. As mentioned, we can improve by making the aggregated values nullable, but it won’t change this behavior.

2reactions
igalklebanovcommented, Oct 25, 2022

avg, max, min & sum are nullable when they don’t have rows to work with.

In a future release you’ll be able to opt-in to hinting they are nullable, and/or wrap them with coalesce.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SelectQueryBuilder | kysely
Executes the query and returns the first result or throws if the query returned no result. By default an instance of NoResultError is...
Read more >
PostgreSQL 11 returns null instead of exception if previous ...
Try to EXPLAIN that query throwing exception, it should just output query plan, but instead it will throw exception - because it decided...
Read more >
sqlite3 can only bind numbers, strings, bigints, buffers, and null ...
In my case I'm testing with both objects and arrays. This is in spite the examples in the docs include array and object...
Read more >
Why does my SELECT query not return null values? [duplicate]
This happens because col IN (1,null) returns TRUE if col=1 , and NULL otherwise (i.e. it can never return FALSE ). Since NOT...
Read more >
sql query should return 0 instead of NULL - CodeProject
SQL. SELECT ISNULL(SUM(Pairs), 0) AS previous_pairs FROM payout WHERE user_id = '100001'.
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