Haskell持久性插入行(如果尚未在数据库中)

Kev*_*vin 6 haskell haskell-platform yesod

我正在尝试使用Yesod并持久创建一个网站.我对如何使用持久API感到有点困惑.

这是我的两张桌子

Feed
    url Text
    UniqueFeed url

Subscription
    feed FeedId
    title Text
    UniqueSubscription feed
Run Code Online (Sandbox Code Playgroud)

我正在尝试创建Feed,如果具有该URL的Feed不存在,然后添加订阅该订阅源,如果订阅尚不存在.

postFeedR :: Handler RepHtml
postFeedR = do
    url <- runInputPost $ ireq urlField "url"
    title <- runInputPost $ ireq textField "title"

    runDB $ do
        feedId <- insertFeed $ UniqueFeed url
        subscriptionId <- insertSubscription feedId title
        return 

    defaultLayout [whamlet| <p>done|]

insertFeed url = do
    f <- insertBy $ UniqueFeed url
    case f of
        Left (Entity uid _) -> uid
        Right (Key uid) -> do
            (Key uid) <- insert $ Feed url
            return uid

insertSubscription feedId title = do
    s <- insertBy $ UniqueSubscription feedId
    case s of 
        Left (Entity uid _) -> uid
        Right (Key uid) -> do
            (Key uid) <- insert $ Subscription feedId title
            return uid
Run Code Online (Sandbox Code Playgroud)

我得到以下错误.我不明白为什么ghc认为insertFeed和insertSubscription的返回值应该是UniqueFeed和UniqueSubscription.我希望这些函数返回新创建的记录的键.

而且,似乎我丢掉了我在案件的每个右边条款中得到的密钥.为什么持久性返回那些键.如果UniqueSubscription不在数据库中,则持久性没有足够的信息来创建新的订阅记录,因为它缺少标题,而不在UniqueSubscription上.

如果有人能给我一些关于如何使用持久API的指示,我真的很感激.

Handler/Home.hs:62:9:
    Kind incompatibility when matching types:
      a0 :: *
      GHandler App App :: * -> *
    Expected type: (a0 -> t0)
                   -> (t0 -> a0 -> m0 a0) -> YesodDB App App (m0 a0)
      Actual type: (a0 -> t0) -> (t0 -> a0 -> m0 a0) -> a0 -> m0 a0
    In a stmt of a 'do' block: feedId <- insertFeed $ UniqueFeed url
    In the second argument of `($)', namely
      `do { feedId <- insertFeed $ UniqueFeed url;
            subscriptionId <- insertSubscription feedId title;
            return }'

Handler/Home.hs:62:9:
    Couldn't match type `YesodPersistBackend App' with `(->)'
    Expected type: (a0 -> t0)
                   -> (t0 -> a0 -> m0 a0) -> YesodDB App App (m0 a0)
      Actual type: (a0 -> t0) -> (t0 -> a0 -> m0 a0) -> a0 -> m0 a0
    In a stmt of a 'do' block: feedId <- insertFeed $ UniqueFeed url
    In the second argument of `($)', namely
      `do { feedId <- insertFeed $ UniqueFeed url;
            subscriptionId <- insertSubscription feedId title;
            return }'

Handler/Home.hs:74:20:
    Couldn't match expected type `Unique Feed'
                with actual type `Database.Persist.Store.PersistValue'
    In the first argument of `return', namely `uid'
    In a stmt of a 'do' block: return uid
    In the expression:
      do { (Key uid) <- insert $ Feed url;
           return uid }

Handler/Home.hs:83:20:
    Couldn't match expected type `Unique Subscription'
                with actual type `Database.Persist.Store.PersistValue'
    In the first argument of `return', namely `uid'
    In a stmt of a 'do' block: return uid
    In the expression:
      do { (Key uid) <- insert $ Subscription feedId title;
           return uid }
Run Code Online (Sandbox Code Playgroud)

Gab*_*iba 7

insertBy不将Unique约束作为参数,getBy更合适.

但是insertUnique与Maybe结果的可能性很小.

postFeedR :: Handler RepHtml
postFeedR = do
    url <- runInputPost $ ireq urlField "url"
    title <- runInputPost $ ireq textField "title"

    runDB $ do
        feedId <- insertFeed url
        _mbSubscriptionId <- insertUnique $ Subscription feedId title
        return ()

    defaultLayout ...

insertFeed url = do
    f <- insertBy $ Feed url
    case f of
        Left (Entity uid _) -> return uid
        Right uid -> return uid
Run Code Online (Sandbox Code Playgroud)