Any way to get rid of the dummy bangs in this expo...
# exposed
p
Any way to get rid of the dummy bangs in this expoed query?
What I'd like to write is a simple query
l
singleOrNull
p
the min itself is nullable for some unknown reason
But yes, singleOrNull is more correct (but doesnát solve the issue with nullable min and max)
l
ah, yeah. sorry, i don't know the answer then.
c
Hi @Pihentagy This is an example of Exposed being unopinionated and following the behavior of the underlying SQL function. Even if a table column is marked as
NOT NULL
, the function
MIN()
can still return SQL
NULL
in certain cases: • When the table is empty • When the
WHERE
cause filters out all rows No rows == empty result set == null return. If Exposed
min()
didn't allow for this possibility by being nullable, it could be a problem in some cases. So, just like with SQL, Exposed leaves it to users to handle this null possibility in whatever way they choose. I personally prefer to handle it on the database side by wrapping
min()
(& other aggregates) in
COALESCE
, but that's just one option:
Copy code
val minCost = Coalesce(measuredAt.min(), intLiteral(0))

// select coalesce(min(measured_at),0), ...
🙌 2
p
Thanks for the explanation. As you can see, I don't have good skills in SQL. 🙈
min
and
max
columns can indeed return
null
values, so one should check for them.