2

所以我打算将 Persistent 0.9.0.1 与 Sqlite 一起使用,我正在阅读教程:http ://www.yesodweb.com/book/persistent

带有插入和查询的简单代码段无法编译:

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

转储拼接显示没有承诺EqShow生成的人的实例,奇怪:

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

我像这样破解它:

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

然后insert无法从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

有任何想法吗?持久性是否适用于 Sqlite?

4

1 回答 1

3

Eq从 0.8 版到 0.9 版发生了变化,我们不再Show默认包含实例。要添加它们,请在下面添加以下行age Int

deriving Show Eq

这本书仍然针对Yesod 0.10版本,但我会在接下来的几天内尝试更新它。

于 2012-04-17T09:55:40.223 回答