【问题标题】:Couldn't match type ‘PersistEntityBackend (Entity a)’ with ‘SqlBackend’无法将类型“PersistEntityBackend (Entity a)”与“SqlBackend”匹配
【发布时间】:2017-06-01 01:28:13
【问题描述】:

我有以下内容:

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 的用途是由于Persistent selectList causing error of "Couldn't match type ‘BaseBackend backend0’ with ‘SqlBackend’"。

我遇到了以下错误:

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

【问题讨论】:

    标签: haskell haskell-persistent


    【解决方案1】:

    将约束添加到insertEnt 的签名中。您还需要一个 PersistEntity 约束。

    insertEnt
      :: ( PersistEntity (Entity a)
         , PersistEntityBackend (Entity a) ~ SqlBackend)
      => Entity a -> IO (Key (Entity a))
    

    要推断出这一点(除了给编译器它间接询问的内容),您可以查看the type of insert

    insert
      :: ( PersistStoreWrite backend
         , MonadIO m
         , PersistRecordBackend record backend)
      => record -> ReaderT backend m (Key record)
    

    我们也有

    type PersistRecordBackend record backend =
      ( PersistEntity record
      , PersistEntityBackend record ~ BaseBackend backend)
    

    此外,在您的应用程序中,您有一些具体的类型:

    backend ~ SqlBackend
    m ~ (some concrete transformer stack on top of IO)
    record ~ Entity a
    

    这些具体类型释放PersistStoreWrite backend 和MonadIO m,并且由于Entity a 仍然大部分是抽象的,因此您只剩下定义PersistRecordBackend 的两个约束。实际上,您可以将该同义词用作速记:

    insertEnt
      :: PersistRecordBackend (Entity a) SqlBackend
      => Entity a -> IO (Key (Entity a))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-26
      • 1970-01-01
      • 2018-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多