haskellesqueleto

Esqueleto query that returns constant value


Is it possible to use esqueleto to create a query that returns a constant value? Like SELECT 1 for example.


Solution

  • Try this:

    import Database.Esqueleto
    
    -- | We have to specialize `val` or else the type inferencer
    -- will complain about the `Esqueleto` instance.
    val_ :: Int -> SqlExpr (Value Int)
    val_ = val
    
    query :: SqlPersistT IO [Value Int]
    query = select $ return (val_ 1)
    

    @chi's comment was close, but the 1 needed to be lifted into a SqlExpr. val is written generically and depends on an Esqueleto class instance. Normally the type inferencer would grab this as soon as you used from and pulled in a SQL table, but since none of that is available here we have to specialize manually.

    Overall a good example of how typeclasses can obfuscate meaning and force people to turn to documentation or forums.