【发布时间】: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