Skip to content
Discussion options

You must be logged in to vote

Hi @morstin, fetchOne has no choice but to return an optional because pretty much any SQL query can return an empty set of rows. It may seem like your situation is guaranteed to return a result since it's an "INSERT … RETURNING", but fetchOne doesn't know that. And further, if you had done "INSERT OR IGNORE … RETURNING", then it would be possible for the query to not return any results.

Since you are already in an error throwing context the fact that have to deal with an optional doesn't seem that bad. You could also create your own helper of .requireOne(db) that unwraps the optional and throws an error if it's nil.

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by morstin
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants