2017-05-29 27 views
1

我運行到下面的編譯錯誤:持久的SelectList導致的錯誤 「無法匹配型 'BaseBackend backend0' 與 '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 

我假設我需要指定runSq的顯式類型lite,儘管我不太確定我在ReaderT backend (NoLoggingT (ResourceT m)) a中爲backend設置了什麼?

+0

這應該工作,你可以發佈整個代碼? – Sibi

回答

1

你可以在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是最常用的一個(未知功能)。如果這是您所需要的,您可以隨意使用更受限制的。

+0

你可能會建議一個資源來閱讀了解這個答案?謝謝! –

+1

我修改了我的答案以便更具描述性。我沒有特別的資源來解釋更高層次的過程。 –

相關問題