Persistent selectList causing error of "Couldn't match type ‘BaseBackend backend0’ with ‘SqlBackend’"

954 Views Asked by At

I'm running into the below compile error:

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

Code:

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

Having a look at the type signature of runSqlite:

runSqlite    
:: (MonadBaseControl IO m, MonadIO m, IsSqlBackend backend)  
=> Text 
-> ReaderT backend (NoLoggingT (ResourceT m)) a 
-> m a

I would assume I need to specify an explicit type for runSqlite, though I'm not too sure what I set for backend in ReaderT backend (NoLoggingT (ResourceT m)) a?

1

There are 1 best solutions below

2
On BEST ANSWER

You can specialize it at 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])

Looking at the type of runSqlite, there is a IsSqlBackend backend constraint to satisfy.

The definition of IsSqlBackend is:

type IsSqlBackend backend =
  (IsPersistBackend backend, BaseBackend backend ~ SqlBackend)

Then look up IsPersistBackend.

Just below the definition of the class, we see that it has three instances:

instance IsPersistBackend SqlWriteBackend
instance IsPersistBackend SqlReadBackend
instance IsPersistBackend SqlBackend

The three types specify backends with various capabilities, with SqlBackend being the most general one (unknown capabilities). Feel free to use a more restricted one if that's all you need.