Haskell 持久库中需要 `Key` 时如何通过 `Int` 获取实体?

How to fetch entity by `Int` when a `Key` is expected in the Haskell persistent library?

我将 Persistent orm 与 scotty web 框架一起使用。

我想通过 id 从 db 中获取值。这些 id 来自 GET 请求

有 "get" 函数接受 "Key Entity" 变量和 returns "Maybe Entity"。

我使用以下代码从数据库中获取值

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-}

这些代码非常难看。但是我不知道怎么做更好

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

PS对不起我的英语不好

你可以使用 toSqlKey