gpt4 book ai didi

haskell - 持久的 selectList 导致错误 "Couldn' t 匹配类型 ‘BaseBackend backend0’ 与 ‘SqlBackend’"

转载 作者:行者123 更新时间:2023-12-03 22:26:30 25 4
gpt4 key购买 nike

我遇到了以下编译错误:

• Couldn't match type ‘BaseBackend backend0’ with ‘SqlBackend’
arising from a use of ‘runSqlite’
The type variable ‘backend0’ is ambiguous
• In the expression: runSqlite ":memory:"
In the expression:
runSqlite ":memory:"
$ do { records <- selectList [UserUsername ==. "abc"] [LimitTo 10];
liftIO $ print (records :: [Entity User]) }
In an equation for ‘selectAll’:
selectAll
= runSqlite ":memory:"
$ do { records <- selectList [UserUsername ==. "abc"] [LimitTo 10];
liftIO $ print (records :: [Entity User]) }

代码:
selectAll :: IO ()
selectAll = runSqlite ":memory:" $ do
records <- selectList [UserUsername ==. "abc"] [LimitTo 10]
liftIO $ print (records :: [Entity User])

看看 runSqlite 的类型签名:
runSqlite    
:: (MonadBaseControl IO m, MonadIO m, IsSqlBackend backend)
=> Text
-> ReaderT backend (NoLoggingT (ResourceT m)) a
-> m a

我假设我需要为 runSqlite 指定一个显式类型,尽管我不太确定我为 backend 设置了什么在 ReaderT backend (NoLoggingT (ResourceT m)) a ?

最佳答案

您可以通过 SqlBackend 专门研究它.

asSqlBackendReader :: ReaderT SqlBackend m a -> ReaderT SqlBackend m a
asSqlBackendReader = id

selectAll :: IO ()
selectAll = runSqlite ":memory:" . asSqlBackendReader $ do
records <- selectList [UserUsername ==. "abc"] [LimitTo 10]
liftIO $ print (records :: [Entity User])

查看 runSqlite的类型,有一个 IsSqlBackend backend满足的约束。

IsSqlBackend 的定义是:
type IsSqlBackend backend =
(IsPersistBackend backend, BaseBackend backend ~ SqlBackend)

那就查 IsPersistBackend .

在类的定义下方,我们看到它有三个实例:
instance IsPersistBackend SqlWriteBackend
instance IsPersistBackend SqlReadBackend
instance IsPersistBackend SqlBackend

这三种类型指定了具有各种功能的后端, SqlBackend是最通用的(未知功能)。如果这就是您所需要的,请随意使用更受限制的一个。

关于haskell - 持久的 selectList 导致错误 "Couldn' t 匹配类型 ‘BaseBackend backend0’ 与 ‘SqlBackend’",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44249710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com