Haskell Persistent:如果我有整数变量的密钥,如何通过密钥从db获取实体?

Dar*_*ver 10 haskell persistent yesod scotty

我使用Persistent orm和scotty web框架.

我希望通过id从db获取值.这些ID来自GET请求

有"获取"功能,它接受"密钥实体"变量并返回"可能实体".

我使用以下代码从db获取值

k <- keyFromValues $ [(PersistInt64 myOwnIntVarFromRequest)]
case k of
    Left _ -> {-some processing-}
    Right x -> do
    t <- liftIO . runDb $ get (x::Key Post) --Post is one of my models
    case t of
        Nothing -> {-processing-}
        Just x -> {-processing-}
Run Code Online (Sandbox Code Playgroud)

这些代码非常丑陋.但我不知道如何做得更好

所以我的问题是如何在不调用keyFromValues的情况下获取"Key Entity"类型的变量.

PS抱歉我的英语不好

Mic*_*man 15

你可以使用toSqlKey.

  • 对于下一个人用谷歌搜索:quasiquoter从引号`persistLowerCase |创建一个'newtype Key TypeName = TypeNameKey {unTypeNameKey :: Int32}`形式的声明.TypeName Id Int32` (4认同)