无法将类型"PersistEntityBackend(实体a)"与"SqlBackend"进行匹配 [英] Couldn't match type ‘PersistEntityBackend (Entity a)’ with ‘SqlBackend’
问题描述
我有以下内容:
asSqlBackendReader :: ReaderT SqlBackend m a -> ReaderT SqlBackend m a
asSqlBackendReader = id
insertEnt :: (Entity a) -> IO (Key (Entity a))
insertEnt x = runWithDb $ do
insert $ x
where runWithDb = runSqlite "test.db" . asSqlBackendReader
asSqlBAckendReader
的目的是由于.
我遇到了以下错误:
• Couldn't match type ‘PersistEntityBackend (Entity a)’
with ‘SqlBackend’
arising from a use of ‘insert’
• In a stmt of a 'do' block: insert $ x
In the second argument of ‘($)’, namely ‘do { insert $ x }’
In the expression: runWithDb $ do { insert $ x }
推荐答案
将约束添加到 insertEnt
的签名中.您还需要一个 PersistEntity
约束.
Add the constraint to the signature of insertEnt
. You'll also need a PersistEntity
constraint.
insertEnt
:: ( PersistEntity (Entity a)
, PersistEntityBackend (Entity a) ~ SqlBackend)
=> Entity a -> IO (Key (Entity a))
要推断出这一点(不只是向编译器提供其间接要求的内容),您可以查看我们也有
type PersistRecordBackend record backend =
( PersistEntity record
, PersistEntityBackend record ~ BaseBackend backend)
此外,在您的应用程序中,您有一些具体类型:
Furthermore, in your application you have some concrete types:
backend ~ SqlBackend
m ~ (some concrete transformer stack on top of IO)
record ~ Entity a
这些具体类型释放 PersistStoreWrite后端
和 MonadIO m
,并且由于 Entity a
仍然大部分都是抽象的,因此您将面临两个约束定义 PersistRecordBackend
.实际上,您可以使用该同义词作为速记:
These concrete types discharge PersistStoreWrite backend
and MonadIO m
, and since Entity a
is still mostly abstract, you are left with the two constraints that define PersistRecordBackend
. You could in fact use that synonym as a shorthand:
insertEnt
:: PersistRecordBackend (Entity a) SqlBackend
=> Entity a -> IO (Key (Entity a))
这篇关于无法将类型"PersistEntityBackend(实体a)"与"SqlBackend"进行匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!