Yesod 书中的第一个 Persistent 示例无法编译 - 找不到 persist

Posted

技术标签:

【中文标题】Yesod 书中的第一个 Persistent 示例无法编译 - 找不到 persist【英文标题】:First Persistent example from Yesod book won't compile -- can't find persist 【发布时间】:2013-06-11 19:39:27 【问题描述】:

我正在尝试运行 Yesod 书(第 10 章)中的 Persistent 示例。我在 .hs 文件中输入了初始示例,创建了一个 cabal 文件,并尝试编译。编译器抱怨它找不到“持久”。我假设该函数 persist 已移至新包(我没有包括在内)或已被弃用,但我不知道是哪个,而且 hoogle 没有对这个问题有任何解释。任何帮助将非常感激。也许我应该回到这本书所基于的 Yesod 版本。我应该为此安装哪个 yesod 平台?谢谢,蒂姆

这是错误信息:

perry$ cabal install
Resolving dependencies...
Configuring chapter10-0.1.0.0...
Building chapter10-0.1.0.0...
Preprocessing executable 'chapter10' for chapter10-0.1.0.0...
[1 of 1] Compiling Main             ( ex1.hs, dist/build/chapter10/chapter10-tmp/Main.o )

ex1.hs:8:55: Not in scope: `persist'
cabal: Error: some packages failed to install:
chapter10-0.1.0.0 failed during the building phase. The exception was:
ExitFailure 1

这是我的 chapter10.cabal 文件:

-- Initial chapter10.cabal generated by cabal init.  For further 
-- documentation, see http://haskell.org/cabal/users-guide/
name:                chapter10
version:             0.1.0.0
license-file:        LICENSE
cabal-version:       >=1.8
build-type:          Simple

executable chapter10
  main-is:             ex1.hs
  -- other-modules:       
  build-depends:       base ==4.5.*
                       , yesod-platform
                       , yesod
                       , persistent-sqlite
                       , transformers
                       , persistent-template
                       , persistent

这是我的 ex1.hs 文件:

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

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persist|
Person
    name String
    age Int Maybe
    deriving Show
BlogPost
    title String
    authorId PersonId
    deriving Show
|]

main :: IO ()
main = withSqliteConn ":memory:" $ runSqlConn $ do
    runMigration migrateAll

    johnId <- insert $ Person "John Doe" $ Just 35
    janeId <- insert $ Person "Jane Doe" Nothing

    insert $ BlogPost "My fr1st p0st" johnId
    insert $ BlogPost "One more for good measure" johnId

    oneJohnPost <- selectList [BlogPostAuthorId ==. johnId] [LimitTo 1]
    liftIO $ print (oneJohnPost :: [Entity BlogPost])

    john <- get johnId
    liftIO $ print (john :: Maybe Person)

    delete janeId
    deleteWhere [BlogPostAuthorId ==. johnId]

这是我的 yesod 和持久包的版本:

perry$ ghc-pkg list| grep -i -e yesod -e persist
    persistent-1.2.0.1
    persistent-sqlite-1.2.0
    persistent-template-1.2.0.1
    yesod-1.2.1
    yesod-auth-1.2.0.1
    yesod-core-1.2.2
    yesod-form-1.3.0
    yesod-persistent-1.2.1
    yesod-platform-1.2.1
    yesod-routes-1.2.0.1
    yesod-static-1.2.0
    yesod-test-1.2.0

【问题讨论】:

【参考方案1】:

根据some older documentation,persist 已被弃用。它似乎已在 1.2.0 版中被删除。

persist :: QuasiQuoter

已弃用:请改用persistUpperCase

【讨论】:

啊...我从没想过要检查旧文档。我只看了 1.2.0.0 和 1.2.0.1。谢谢你为我着想!

以上是关于Yesod 书中的第一个 Persistent 示例无法编译 - 找不到 persist的主要内容,如果未能解决你的问题,请参考以下文章

退出代码 11 安装 Persistent (Yesod)

如何使用 Yesod/Persistent 创建外键约束?

如何在 Yesod / Persistent 中正确使用 runDB

Yesod/Persistent 实体派生 Show

Yesod/Persistent 字段与 Eq

使用 Yesod 和 Persistent 类型不匹配