Why does `sum` return `SQL<string | null>`?
I was trying to use the
sum aggregate function and I noticed that it returns a string. Why is that?
6 Replies
@AstroBear
If you are using drizzle function
sum(), it is designed this way. If you are using sql, drizzle cannot accurately determine the type without user input. So the best way is to determine type by yourself
Thanks, I was using the drizzle
sum agg helper. I ended up just using Number() at the end, but I was wondering why it was decided it had to return a stringThis is drizzle
sum agg helper, so it is just designed this way
Got it, ty. I guess it is mapped to
String for some sort of compatibility."Just designed this way", is not a great answer. I will also use this workaround but it would be great if someone who knows the history of
sum can answer why it returns string | null.Because the
sum function can theoretically return null
If no columns are returned by your query, sum will return null
A string was used because drizzle cannot warrantee that the number returned by the database will be representable in a JS number
It could be a bigint