[英]YesodDB, how to put types correctly
我已經創建了一個用於加載一些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'
我認為類型聲明中的這個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) }
我記得在 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
就是答案
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.