本文介绍了在Maybe关系上运行join的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个模型
Assignment
blah Text
....
和模型
File
assignmentId AssignmentId Maybe
...
在连接查询中获取与分配相关联的所有文件。我试过Esqueleto和 runJoin
与 selectOneMany
但没有运气,所以我考虑不使用连接,或使用rawSql。这真的不是一个好主意,但我不能想出这一点。
and I want to get all the files associated with an assignment in a join query. I have tried Esqueleto and runJoin
with selectOneMany
but haven't had any luck, so I am considering not using a join, or using rawSql. That really doesn't seem like a good idea, but I can't figure this out. Is there any support for that feature?
推荐答案
更新,工作示例:
{-# 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
这篇关于在Maybe关系上运行join的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!