Skirmantas Kligys
Skirmantas Kligys

Reputation: 826

Newbie problems with Persistent

So I am planing to use Persistent 0.9.0.1 with Sqlite, and I am going through the tutorial: http://www.yesodweb.com/book/persistent

The simple snippet with insert and query fails to compile:

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

share [mkPersist sqlSettings, mkSave "entityDefs"] [persist|
Person
    name String
    age Int
|]

main = withSqliteConn ":memory:" $ runSqlConn $ do
    runMigration $ migrate entityDefs (undefined :: Person) -- this line added: that's it!
    michaelId <- insert $ Person "Michael" 26
    michael <- get michaelId
    liftIO $ print michael
-- STOP

Dumping splices shows no promised Eq, Show instances for generated Person, strange:

data PersonGeneric (backend :: (* -> *) -> * -> *)
    = Person {personName :: String, personAge :: Int}
type Person =
    PersonGeneric Database.Persist.GenericSql.Raw.SqlPersist

I hack it like this:

    liftIO $ putStrLn $ "name: " ++ (personName $ fromJust michael) ++ ", age: " ++ (show $ personAge $ fromJust michael)

And then insert fails to get the primary key back from Sqlite:

Migrating: CREATE TABLE "person"("id" INTEGER PRIMARY KEY,"name" VARCHAR NOT NULL,"age" INTEGER NOT NULL)
proto: Pattern match failure in do expression at Database/Persist/GenericSql.hs:109:25-45

Any ideas? Does Persistent work with Sqlite?

Upvotes: 2

Views: 360

Answers (1)

Michael Snoyman
Michael Snoyman

Reputation: 31315

There was a change from version 0.8 to 0.9 where we no longer include Eq and Show instances by default. To add them in, add the following line underneath age Int:

deriving Show Eq

The book is still targeted at version 0.10 of Yesod, but I'll try to update it in the next few days.

Upvotes: 3

Related Questions