Yesod 和 MySQL 连接问题

Yesod and MySQL connection issue

我一直在网上寻找将 Yesod - Persistent 连接到 MySQL 数据库的工作示例,我找到了这个 resource on GitHub

{-# LANGUAGE EmptyDataDecls             #-}
{-# LANGUAGE FlexibleContexts           #-}
{-# LANGUAGE GADTs                      #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses      #-}
{-# LANGUAGE OverloadedStrings          #-}
{-# LANGUAGE QuasiQuotes                #-}
{-# LANGUAGE TemplateHaskell            #-}
{-# LANGUAGE TypeFamilies               #-}
import           Control.Monad.IO.Class  (liftIO)
import           Control.Monad.Logger    (runStderrLoggingT)
import           Database.Persist
import           Database.Persist.MySQL
import           Database.Persist.TH

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

connectionInfo = defaultConnectInfo { connectPort = 5000,
                                      connectPassword = "password",
                                      connectDatabase = "database"}

main :: IO ()
main = runStderrLoggingT $ withMySQLPool connectionInfo 10 $ \pool -> liftIO $ do
         flip runSqlPersistMPool pool $ do
           printMigration migrateAll

当我使用 ghci 尝试此代码时,出现以下错误:

Prelude> :l test3.hs 
[1 of 1] Compiling Main             ( test3.hs, interpreted )

test3.hs:35:27:
    Couldn't match type `persistent-2.2.2.1:Database.Persist.Sql.Types.SqlBackend'
                  with `SqlBackend'
    Expected type: Migration
      Actual type: persistent-2.2.2.1:Database.Persist.Sql.Types.Migration
    In the first argument of `printMigration', namely `migrateAll'
    In a stmt of a 'do' block: printMigration migrateAll
    In the second argument of `($)', namely
      `do { printMigration migrateAll }'
Failed, modules loaded: none.

我陷入其中。

我的另一个问题是:有人使用过 Yesod + MySQL 吗?如果是,我想知道这是一个好的选择还是一个有问题的选择。

您似乎安装了一个软件包的两个版本。如果您使用 Stack 安装,它将确保您在范围内只有一个版本的每个包。我在 this blog post.

中详细描述了这种情况