[英]YesodDB, how to put types correctly
I have created a function for loading some Entities
but I am having some trouble to understand how I should put its type declaration, so I took this Yesod book chapter about Yesod monads, to understand it better, and I came to this snippet:我已经创建了一个用于加载一些Entities
的函数,但是我在理解如何放置它的类型声明时遇到了一些麻烦,所以我阅读了关于 Yesod monad 的Yesod 书章节,以便更好地理解它,然后我来到了这个片段:
-- type Distance = Int
worksByNhood :: AdrNhoodId -> Int64 -> Int64 -> YesodDB App [(Entity Work, Int, [Entity WImage])]
worksByNhood nId offset' limit' = do
works <- select $ from $ \(w `InnerJoin` d) -> do
on $ d ^. AdrDistanceNhood2 ==. w ^. WorkNhood
where_ (d ^. AdrDistanceNhood1 ==. val nId)
orderBy [asc (d ^. AdrDistanceDistance)]
offset offset'
limit limit'
return (w, d ^. AdrDistanceDistance)
works' <- forM works $ \(w@(Entity wId _), d) -> do
images <- select $ from $ \wi -> do
where_ (wi ^. WImageWork ==. val wId)
return wi
return (w, d, images);
return works'
I think this Int
in the type declaration needs to be converted someway, to be more specific, I am getting this error:我认为类型声明中的这个Int
需要以某种方式转换,更具体地说,我收到这个错误:
Select.hs:20:12:
Couldn't match type ‘Database.Esqueleto.Value Distance’ with ‘Int’
Expected type: (Entity Work, Distance, [Entity WImage])
Actual type: (Entity Work,
Database.Esqueleto.Value Distance,
[Entity WImage])
In the first argument of ‘return’, namely ‘(w, d, images)’
In a stmt of a 'do' block: return (w, d, images)
In the expression:
do { images <- select $ from $ \ wi -> do { ... };
return (w, d, images) }
I remembered there is a chapter about joins in the great Yesod book , so I read it again and found the solution, maybe my code may help others, so here it goes:我记得在 Yesod 的书里有一个关于 join的章节,所以我又看了一遍,找到了解决方案,也许我的代码可以帮助别人,所以这里是:
module Select where
import Import hiding((==.), on, Value)
import Database.Persist.Sql (toSqlKey) -- Useful when I want to explicitly use primary keys
import Database.Esqueleto
-- type Distance = Int
worksByNhood :: AdrNhoodId -> Int64 -> Int64 -> YesodDB App [(Entity Work, Value Distance, [Entity WImage])]
worksByNhood nId offset' limit' = do
works <- select $ from $ \(w `InnerJoin` d) -> do
on $ d ^. AdrDistanceNhood2 ==. w ^. WorkNhood
where_ (d ^. AdrDistanceNhood1 ==. val nId)
orderBy [asc (d ^. AdrDistanceDistance)]
offset offset'
limit limit'
return (w, d ^. AdrDistanceDistance)
forM works $ \(w@(Entity wId _), d) -> do
images <- select $ from $ \wi -> do
where_ (wi ^. WImageWork ==. val wId)
return wi
return (w, d, images)
Value Distance
at the type declaration is the answer类型声明处的Value Distance
就是答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.