0

我一直在关注 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
                    )
4

1 回答 1

0

多亏了这个要点,我找到了答案

这是完整的示例:

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
                    )
于 2016-01-08T06:14:59.360 回答