所以我计划将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
转储拼接显示生成的Person没有承诺的Eq
,Show
个实例,奇怪:
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)
然后插入无法从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
有什么想法吗? Persistent是否适用于Sqlite?
答案 0 :(得分:3)
从版本0.8更改为0.9,默认情况下我们不再包含Eq
和Show
个实例。要添加它们,请在age Int
下面添加以下行:
deriving Show Eq
这本书仍然针对Yesod版本0.10,但我会在接下来的几天内尝试更新它。