所以我计划将 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
转储拼接显示没有 promise 的Eq
,Show
生成的Person实例,奇怪:
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
有什么想法吗?持久性可以与 Sqlite 一起使用吗?
最佳答案
从版本 0.8 到 0.9 进行了更改,默认情况下我们不再包含 Eq
和 Show
实例。要添加它们,请在 age Int
下面添加以下行:
deriving Show Eq
这本书仍然针对 Yesod 0.10 版本,但我会在接下来的几天内尝试更新它。
关于sqlite - 持久性的新手问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10185339/