-
Hi there, I want to implent an upsert and I understand that the db transaction fetchOne(db) can fail - but not really why it should return an optional. Am I using returning + fetchOne(db) incorrectly? Below code does not compile as it does not consider the optional return from
So basically if I want a non-optional Record, I need to unwrap and introduce my own error message - without really knowing why it did fail 🧐:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi @morstin, 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 |
Beta Was this translation helpful? Give feedback.
-
Thank you @mbrandonw - I think the
|
Beta Was this translation helpful? Give feedback.
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", butfetchOne
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'snil
.