Yesod /持久性一对一查询 [英] Yesod/Persistent one-to-one query
问题描述
User
ident Text
密码文本也许
UniqueUser ident
问题
title文本
asker UserId Eq
我列出了问题
s,并希望检索 User
秒。我会如何去做这件事?
我想过加入,但这些是一对多的,而不是一对一的(我想它不会没关系,但我想要一个更简单的解决方案)。手动进行连接也是一种选择,但我担心性能 - 我有
questions< - runDB $ selectList [QuestionTitle!=。 ] [LimitTo 10]
let askerIds = map(\(Entity _q) - > questionAsker q)questions
askers< - sequence $ map(runDB。get)askerIds
让questionsAndAskers = zip问题提问者
但我担心使用 runDB在
(它不会为每个用户向数据库发出一个单独的请求吗?) map
中
有没有更好的/更习惯的方式来达到这个目的? 但是我会将整个内容粘贴在 runDB
中:
runDB $ selectList [QuestionTitle!=。 ]] [LimitTo 10]>> = mapM(\qe @(Entity _q) - > do
asker< - get $ questionAsker q
return(qe,asker))
Say, in Yesod/Persistent, I have models setup like so:
User
ident Text
password Text Maybe
UniqueUser ident
Question
title Text
asker UserId Eq
And I have a list of Question
s, and would like to retrieve the corresponding list of User
s. How would I go about doing this?
I've thought about joins, but those are one-to-many, not one-to-one (I suppose it doesn't matter, but I'd like a simpler solution). Manually doing the join is also an option, but I'm worried about performance - I have
questions <- runDB $ selectList [QuestionTitle !=. ""] [LimitTo 10]
let askerIds = map (\(Entity _ q) -> questionAsker q) questions
askers <- sequence $ map (runDB . get) askerIds
let questionsAndAskers = zip questions askers
but I'm worried about using runDB
in the map
(wouldn't it make a separate request to the database for each user?)
Is there a better/more idiomatic way to achieve this?
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))
这篇关于Yesod /持久性一对一查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!