2014-05-21 34 views
0

我嘗試從外部屬性列表中檢索實體列表時出現以下錯誤。持久性'get`調用導致模糊類型變量

Handler/ContactList.hs:21:57: 
Couldn't match type `PersistMonadBackend m0' 
       with `persistent-1.3.0.6:Database.Persist.Sql.Types.SqlBackend' 
The type variable `m0' is ambiguous 
Possible fix: add a type sig`enter code here`nature that fixes these type variable(s) 
Expected type: PersistEntityBackend User 
    Actual type: PersistMonadBackend m0 
In the expression: `enter code here`get 
In the expression: (get $ ((contactContact val) :: UserId)) 
In the first argument of `map', namely 
    `(\ (Entity key val) -> (get $ ((contactContact val) :: UserId)))'  

下面的代碼獲取一個List實體,它與聯繫人具有一對多的關係。在Contact模型中,有一個名爲contact的屬性來表示用戶的外鍵約束(Conctact的類型爲UserId)。 我試圖檢索聯繫人列表(userIds列表)並執行地圖獲取來檢索用戶實體列表。

爲了我的理解,在使用UserId類型的聯繫實體時存在類型泛化的問題,但我無法弄清楚它可以使用的正確類型。

getContactsR :: Handler Html 
getContactsR = do 
    muser <- maybeAuth 
    case muser of 
    Just (Entity userId user) -> do 
     (list, contacts) <- runDB $ do 
      maybeList <- getBy (UniqueList userId) 
      case maybeList of 
      Just (Entity listId list) -> do 
       contacts' <- selectList [ContactList ==. listId] []       
       let contacts = map (\(Entity key val) -> (get $ ((contactContact val) :: UserId))) contacts' 
       return (list, contacts') 
      Nothing -> error "Could not retrieve contact list" 
     defaultLayout $(widgetFile "contacts") 
    Nothing -> do 
    setMessage $ toHtml ("Error getting contact list" :: Text) 
    redirect HomeR 

在此先感謝

回答

3

我認爲你需要更換:

let contacts = map (\(Entity key val) -> (get $ ((contactContact val) :: UserId))) contacts' 

有:

contacts <- mapM (\(Entity key val) -> (get $ ((contactContact val) :: UserId))) contacts' 

(是的,這裏的持續出現錯誤信息是可怕的,我們」重新爲Persistent 2.0工作)。我認爲UserId類型的註釋可能不是n具完整。