【问题标题】:Persistent: Convert Text to Key持久性:将文本转换为键
【发布时间】:2013-03-11 02:29:29
【问题描述】:

我正在使用持久化和持久化-mysql。我有一个 Monad SqlM

type SqlM a = SqlPersist (ResourceT IO) a) 

在我的函数内部

testFun :: T.Text -> SqlM ()
testFun someId = ...

我可以使用查询数据库

entity <- selectFirst [SomeField ==. someId]

但我想按 ID 选择实体。我必须将 someId 转换/打包为 Key - Type。我知道这不是这样做的方法,但我试过了:

entity <- get $ Key { unKey = PersistInt64 (read $ T.unpack someId) }

这失败了:

Couldn't match type `PersistEntityBackend
                       (Entity (DBTableGeneric backend0))'
              with `Database.Persist.GenericSql.Raw.SqlBackend'
The type variable `backend0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Expected type: Key (Entity (DBTableGeneric backend0))
  Actual type: KeyBackend
                 Database.Persist.GenericSql.Raw.SqlBackend
                 (Entity (DBTableGeneric backend0))
In the second argument of `($)', namely
  `Key {unKey = PersistInt64 (read $ T.unpack someId)}'

有什么想法吗?

【问题讨论】:

    标签: mysql haskell persistent yesod


    【解决方案1】:

    我通常使用fromPathPiece 进行这种转换。至于您的错误消息,您可能只需添加一个类型签名以向编译器阐明您正在使用的类型。

    【讨论】:

    • 我必须将类型签名添加到什么?我尝试将它添加到键 KeyBackend SqlBackend 实体,但我仍然收到上面显示的错误。
    • 试试(fromPathPiece someId :: Maybe DBTableId)
    • 非常感谢您的快速帮助,成功了!
    猜你喜欢
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多