2017-04-01 92 views
3

我想通过Yesod中的id获得记录。我的代码是:Haskell - 无法将类型'PersistEntityBackend record0'与'SqlBackend'相匹配

getEditActorR :: Handler Html 
getEditActorR = do 
    actorId <- runInputGet $ ireq intField "id" 
    actor <- runDB $ get $ Key $ PersistInt64 (fromIntegral actorId) 
    defaultLayout $ do 
     $(widgetFile "actor-edit") 

我得到的错误是:

• Couldn't match type ‘PersistEntityBackend record0’ 
       with ‘SqlBackend’ 
    arising from a use of ‘get’ 
    The type variable ‘record0’ is ambiguous 
• In the second argument of ‘($)’, namely 
    ‘get $ Key $ PersistInt64 (fromIntegral actorId)’ 
    In a stmt of a 'do' block: 
    actor <- runDB $ get $ Key $ PersistInt64 (fromIntegral actorId) 
    In the expression: 
    do { actorId <- runInputGet $ ireq intField "id"; 
     actor <- runDB $ get $ Key $ PersistInt64 (fromIntegral actorId); 
     defaultLayout $ do { (do { ... }) } } 

我该如何解决呢?

回答

3

我做的第一件事就是运行stack ghci

然后我运行:info Actor,其中Actor是我的PersistEntity的名称。

除其他事项外,还有:

newtype instance Key Actor = ActorKey {unActorKey :: Int} 

所以我写了:

maybeActor <- runDB $ get $ ActorKey actorId 
case maybeActor of 
    Just actor -> defaultLayout $ do 
     $(widgetFile "actor-edit") 
    Nothing -> defaultLayout $ do 
     $(widgetFile "actor-new") 
相关问题