我一直关注Yesod的SQL Joins中的示例,我想在Inner Join示例中添加一个where_
子句 - 它应该只显示作者ID高于1的博客
我认为我的错误可能是我正在做E.val 1
,但不知道如何将Key
传递给它
getHomeR :: Handler Html
getHomeR = do
blogs <- runDB
$ E.select
$ E.from $ \(blog `E.InnerJoin` author) -> do
E.on $ blog ^. BlogAuthor E.==. author ^. AuthorId
E.where_ $ author ^. AuthorId E.>. (E.val 1) -- how to pass ID 1
E.limit 5
return
( blog ^. BlogId
, blog ^. BlogTitle
, author ^. AuthorName
)
答案 0 :(得分:0)
由于gist
,我找到了答案以下是完整示例:
import Database.Persist.Sql (toSqlKey)
import qualified Database.Esqueleto as E
import Database.Esqueleto ((^.))
getHomeR :: Handler TypedContent
getHomeR = do
let authorID = toSqlKey 1 :: AuthorId -- cast 1 to the expected key
let blogsSrc =
E.selectSource
$ E.from $ \(blog `E.InnerJoin` author) -> do
E.on $ blog ^. BlogAuthor E.==. author ^. AuthorId
E.where_ $ author ^. AuthorId E.>. E.val authorID
return
( blog ^. BlogId
, blog ^. BlogTitle
, author ^. AuthorName
)