我使用持久库定义了以下实体:
ClapEntity
Id Text
count Int
userId UserEntityId
deriving Show
UserEntity
Id Text
name Text
email Text
deriving Show
我想存储数据模型ClapEntity
中的新内容ClapCreate
:
newtype UserId = UserId {unUserId :: UUID}
deriving (Eq, Show, ToJSON, FromJSON)
data ClapCreate = ClapCreate
{ userId :: UserId,
count :: Int
}
deriving (Eq, Show)
-- Insert a new clap record into the database
storeClap :: ConnectionPool -> ClapCreate -> IO ClapId
storeClap pool clap = do
let clapEntity = clapCreateToEntity clap
key <- generateClapEntityKey
cid <- runSql (insertKey key clapEntity) pool
case toUUID (unClapEntityKey key) of
Left err -> error $ show err
Right uuid -> return $ ClapId uuid
clapCreateToEntity :: ClapCreate -> ClapEntity
clapCreateToEntity (ClapCreate (UserId uid) cnt) =
ClapEntity
cnt
(UserEntityId (UUID.toText uid)) -- ISSUE HERE
我不知道如何UserEntityId
从给定的内容创建新的内容uid
:(
另外,我不想获取UserEntity
第一个并使用返回的键,只想创建一个新的外键。我遗漏了一些东西...
附加问题
顺便问一下,我该如何cid
使用case toUUID (unClapEntityKey key) of
?
你用 来引用一个对象
α
,αKey
因此在这种情况下UserEntityKey
:也
UserEntityId
存在,但这是一个没有任何参数的数据构造函数:它用于引用表达式中的列,例如selectList [UserEntityId ==. "c86aed00-023a-4368-89ce-56e5cbcd810e"]