Yesod/Persistent one-to-one query

后端 未结 2 994
猫巷女王i
猫巷女王i 2021-01-21 02:02

Say, in Yesod/Persistent, I have models setup like so:

User
    ident Text
    password Text Maybe
    UniqueUser ident
Question
    title Text
    asker UserId          


        
相关标签:
2条回答
  • 2021-01-21 02:42

    How about:

    questions <- runDB $ selectList [QuestionTitle !=. ""] [LimitTo 10]
    let askerIds = map (\(Entity _ q) -> questionAsker q) questions
    askers <- runDB $ selectList [UserId <-. askerIds] []
    let questionsAndAskers = zip questions askers
    

    That seems like it should hit the DB once for the users.

    0 讨论(0)
  • 2021-01-21 02:58

    I haven't type-checked this yet, but I would stick the whole thing inside of runDB:

    runDB $ selectList [QuestionTitle !=. ""] [LimitTo 10] >>= mapM (\qe@(Entity _ q) -> do
        asker <- get $ questionAsker q
        return (qe, asker))
    
    0 讨论(0)
提交回复
热议问题