Search code examples
haskellyesodhaskell-persistent

Haskell - Couldn't match type ‘PersistEntityBackend record0’ with ‘SqlBackend’


I am trying to get a record by id in Yesod. My code is:

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

The error I get is:

• 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 { ... }) } }

How can I fix that?


Solution

  • First thing I did was to run stack ghci.

    Then I run :info Actor, where Actor is the name of my PersistEntity.

    Among other things, there was:

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

    So I wrote:

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