我有一个模特
Assignment
blah Text
....
和模型
File
assignmentId AssignmentId Maybe
...
我希望在连接查询中获取与分配相关联的所有文件。我已经尝试了Esqueleto和runJoin
selectOneMany
,但没有运气,所以我考虑不使用连接或使用rawSql。这真的不是一个好主意,但我无法弄清楚这一点。是否有任何支持该功能?
答案 0 :(得分:2)
更新,工作示例:
{-# LANGUAGE PackageImports, OverloadedStrings, ConstraintKinds #-}
module Handler.HTest where
import Import
import "esqueleto" Database.Esqueleto as Esql
import "monad-logger" Control.Monad.Logger (MonadLogger)
import "resourcet" Control.Monad.Trans.Resource (MonadResourceBase)
import qualified Data.List as L
getFilesByAssignment :: (PersistQuery (SqlPersist m), MonadLogger m
, MonadResourceBase m) =>
Text -> SqlPersist m [Entity File]
getFilesByAssignment myAssign = do
result <- select $
from $ \(assign `InnerJoin` file) -> do
on (just (assign ^. AssignmentId)
Esql.==. file ^. FileAssignmentId)
where_ (assign ^. AssignmentBlah Esql.==. val myAssign)
return (assign, file)
return $ map snd (result :: [(Entity Assignment, Entity File)])
(.$) = flip ($)
getTestR :: Handler RepHtml
getTestR = do
entFiles <- runDB $ getFilesByAssignment "test"
defaultLayout $ do
setTitle "Test page"
entFiles .$ map (show . unKey . entityKey)
.$ L.intercalate ", "
.$ toHtml
.$ toWidget