Types of Coercion in Persistent

This is a "hello world" attempt, which is currently failing - I'm just trying to run a query selectListin a SqLite database with the following code:

Database.Persist.Sqlite> runSqlite "database.sqlite" $ selectList [] [LimitTo 10]

<interactive>:46:1:
    Couldn't match expected type ‘SqlBackend’
                with actual type ‘PersistEntityBackend val0’
    The type variable ‘val0’ is ambiguous
    In the first argument of ‘print’, namely ‘it’
    In a stmt of an interactive GHCi command: print it

It almost seems too simple to ruin ... where did I go wrong?

+4
source share
2 answers

As you probably already know, one of Haskell's strengths is strong typing. The persistent-sqlite package does this to the extreme (which, in my opinion, is good), requiring that the records in the table have their own data type.

For example, if you have a table that stores fruits that look like

_______________________
|Fruit ID | Fruit Name|
-----------------------
|   0     | "apple"   |
|   1     | "orange"  |
-----------------------

persistent-sqlite, Fruit

data Fruit = Fruit { fruitName::String }

, . , , Haskell magic , .

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Fruit
    name String
    deriving Show
|]

, . , , ,

The type variable ‘val0’ is ambiguous

: " , sql".

print (fruits :: [Entity Fruit])

, , GHC. , .

{-# LANGUAGE GADTs                      #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses      #-}
{-# LANGUAGE OverloadedStrings          #-}
{-# LANGUAGE QuasiQuotes                #-}
{-# LANGUAGE TemplateHaskell            #-}
{-# LANGUAGE TypeFamilies               #-}
import           Control.Monad.IO.Class  (liftIO)
import           Database.Persist.Sqlite
import           Database.Persist.TH

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Fruit
    name String
    deriving Show
|]


main :: IO ()
main = runSqlite "fruitDb.sqlite" $ do
    fruits <- selectList [] [LimitTo 10]
    liftIO $ print (fruits :: [Entity Fruit])

, sqlite db, .

> sqlite3 fruitDb.sqlite

sqlite> create table fruit (id, name);
sqlite> insert into fruit values (0, "apple");
sqlite> insert into fruit values (1, "orange");
+5

: The type variable ‘backend0’ is ambiguous Sqlite. () , backend , BaseBackend backend = SqlBackend.

, : SqlBackend, SqlWriteBacknde SqlReadBackend. , , , , , , .

, - ( :: _ , ), runMigration , SqlBackend.

+3

All Articles