4

Is it possible to write

select min(next) from participant;

as an esqueleto query?

UPDATE

I discovered the min_ function. However, the code

nextMessageTime =
  from $ \p -> min_ (p ^. ParticipantNext)

apparently has some ambiguous types:

Could not deduce (Database.Esqueleto.Internal.Language.From
                    query expr backend (query (Entity (ParticipantGeneric backend0))))
  arising from the ambiguity check for ‘nextMessageTime’
from the context (Database.Esqueleto.Internal.Language.From
                    query expr backend (query (Entity (ParticipantGeneric backend2))),
                  Esqueleto query1 query backend1)
  bound by the inferred type for ‘nextMessageTime’:
             (Database.Esqueleto.Internal.Language.From
                query expr backend (query (Entity (ParticipantGeneric backend2))),
              Esqueleto query1 query backend1) =>
             query (Value (Maybe Int64))
  at Presta/DB/Queries.hs:(15,1)-(16,42)
The type variable ‘backend0’ is ambiguous
When checking that ‘nextMessageTime’
  has the inferred type ‘forall (query :: * -> *)
                                (expr :: * -> *)
                                backend
                                (query1 :: * -> *)
                                backend1
                                backend2.
                         (Database.Esqueleto.Internal.Language.From
                            query expr backend (query (Entity (ParticipantGeneric backend2))),
                          Esqueleto query1 query backend1) =>
                         query (Value (Maybe Int64))’
Probable cause: the inferred type is ambiguous
Roman Cheplyaka
  • 37,738
  • 7
  • 72
  • 121

1 Answers1

2

I managed to get this working using persistent's rawSql:

nextMessageTime = do
  [Single t] <-
    rawSql
      "SELECT min(next) FROM participant"
      []
  return (t :: Int64)

(But mind the inexhaustive pattern if you're copying this code.)

Roman Cheplyaka
  • 37,738
  • 7
  • 72
  • 121