【问题标题】:How to get the associated data using the foreign key如何使用外键获取关联数据
【发布时间】:2019-06-04 04:24:53
【问题描述】:

我正在尝试编写一个函数来查找用户的个人资料,或者如果它不存在则创建一个。

我已使用 getBy 和 selectFirst 来获取给定用户的个人资料,但我收到此错误:

无法将类型“HandlerFor site0”与“Key”匹配

我正在使用带有 postgres 的脚手架站点。

这是我的模型(用户和个人资料是一对一的关系)

User
    email Text
    password Text Maybe
    verkey Text Maybe
    verified Bool
    UniqueUser email                                                                                                   
    deriving Typeable

Profile 
    name Text                                                                                                          
    userId UserId
    UniqueName name
    UniqueUserId userId
    deriving Typeable

功能如下:

getOrCreateProfile :: UserId -> ProfileId
getOrCreateProfile userId = do
    mProfile <- runDB $ getBy $ UniqueUserId userId                                                                    
    case mProfile of
        Just (Entity pid _) -> return pid
        Nothing  -> undefined -- insert profile

我得到的错误是:

    • Couldn't match type ‘HandlerFor site0’ with ‘Key’
      Expected type: Key (Maybe (Entity Profile))
        Actual type: HandlerFor site0 (Maybe (Entity Profile))
    • In a stmt of a 'do' block:
        mProfile <- runDB $ getBy $ UniqueUserId userId
      In the expression:
        do mProfile <- runDB $ getBy $ UniqueUserId userId
           case mProfile of
             Just (Entity pid _) -> pid
             Nothing -> undefined
      In an equation for ‘getOrCreateProfile’:
          getOrCreateProfile userId
            = do mProfile <- runDB $ getBy $ UniqueUserId userId
                 case mProfile of
                   Just (Entity pid _) -> pid
                   Nothing -> undefined
   |
45 |     mProfile <- runDB $ getBy $ UniqueUserId userId
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

我做错了什么?执行此查询的正确方法是什么?

【问题讨论】:

    标签: haskell yesod one-to-one persistent


    【解决方案1】:

    如果你看runDB's type signature

    runDB :: YesodDB site a -> HandlerFor site a
    

    你会看到问题所在。

    Yesod 要求您做一些相当复杂的事情来实际使用查询的结果——它不仅仅是一个(可能的结果)。你可以找到例子here;特别是这部分:

    people <- runDB $ selectList [] [Asc PersonAge]
    defaultLayout
            [whamlet|
                <ul>
                    $forall Entity personid person <- people
                        <li>
                            <a href=@{PersonR personid}>#{personFirstName person}
            |]
    

    希望这会有所帮助。

    【讨论】:

    • 谢谢!我更改了类型签名,它正在编译。
    【解决方案2】:

    正在编译

    getOrCreateProfile :: UserId -> Handler ProfileId
    getOrCreateProfile userId = runDB $ do
        mProfile <- getBy $ UniqueUserId userId
        case mProfile of
            Just (Entity pid _) -> return pid
            Nothing  -> insert $ Profile getTempProfile userId
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-16
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      • 2021-05-20
      相关资源
      最近更新 更多