导致 "Couldn't match type ‘BaseBackend backend0’ with ‘SqlBackend’" 错误的持久性 selectList
Persistent selectList causing error of "Couldn't match type ‘BaseBackend backend0’ with ‘SqlBackend’"
我 运行 遇到以下编译错误:
• 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 指定一个显式类型,尽管我不太确定我在 ReaderT backend (NoLoggingT (ResourceT m)) a
?
中为 backend
设置了什么
您可以在 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
。
就在 class 的定义下面,我们看到它有三个实例:
instance IsPersistBackend SqlWriteBackend
instance IsPersistBackend SqlReadBackend
instance IsPersistBackend SqlBackend
这三种类型指定了具有各种功能的后端,SqlBackend
是最通用的一种(功能未知)。如果您只需要这些,请随意使用更受限制的。
我 运行 遇到以下编译错误:
• 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 指定一个显式类型,尽管我不太确定我在 ReaderT backend (NoLoggingT (ResourceT m)) a
?
backend
设置了什么
您可以在 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
。
就在 class 的定义下面,我们看到它有三个实例:
instance IsPersistBackend SqlWriteBackend
instance IsPersistBackend SqlReadBackend
instance IsPersistBackend SqlBackend
这三种类型指定了具有各种功能的后端,SqlBackend
是最通用的一种(功能未知)。如果您只需要这些,请随意使用更受限制的。