Correct way to do a “join” in persist with yesod

我只是一个虾纸丫 提交于 2019-12-03 07:45:25

There is limited join support in persistent at this time, and I believe it is SQL only.

I have a couple of helpers which I use for simple cases. They can be found here. It's not a true JOIN, it selects once per table then builds a list of tuples representing "joined" rows with an element from each.

Given your models and that helper, you should able to do something like:

records <- runDB $ do
    sessions <- selectList [] []
    players  <- selectList [] []
    tables   <- selectList [] []

    return $ joinTables3 gamingSessionPlayer gamingSessionTable sessions players tables

forM records $ \(session, player, table) -> do
    --
    -- ...
    --

Only cases where a record exists in all three tables will be returned (so it's an INNER JOIN), but you might want to pre-filter for efficiency too.

unohoo

For future reference, for sql you can use esqueleto or rawSQL to do joins - see this answer Baffled by selectOneMany in Yesod

If you wanted to use a join, in esqueleto your query would look something like:

select $ from $ \(gamingSession `InnerJoin` player) -> do 
    on (gamingSession ^. GamingSessionPlayer ==. player ^. PlayerId)
    where_ $ isNothing $ gamingSession ^. GamingSessionEnd
    orderBy [asc (gamingSession ^. GamingSessionTable)] 
    return (gamingSession, player ^. PlayerId)

This would return a (Entity GamingSession, PlayerId) tuple

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!