Yesod /持久性一对一查询 [英] Yesod/Persistent one-to-one query

查看:94
本文介绍了Yesod /持久性一对一查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Yesod / Persistent中,我有模型设置如下所示:

  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 Questions, and would like to retrieve the corresponding list of Users. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆