gpt4 book ai didi

sqlite - Persistent 的新手问题

转载 作者:行者123 更新时间:2023-12-03 17:50:10 24 4
gpt4 key购买 nike

所以我打算在 Sqlite 中使用 Persistent 0.9.0.1,我正在阅读教程: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生成的人的实例,奇怪的:
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.8 版到 0.9 版发生了变化,我们不再包含 EqShow默认情况下的实例。要添加它们,请在 age Int 下添加以下行:

deriving Show Eq

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

关于sqlite - Persistent 的新手问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10185339/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com