1

我正在学习 Haskell,所以我决定编写一个 Web 应用程序。我确实选择了 PostgreSQL Simple 来处理数据库。我成功连接到它并尝试了简单的数学运算,但在尝试将记录映射到数据时遇到了问题。此代码无法编译:

module Handlers.SurveyReplies where

import Database.PostgreSQL.Simple

data AnswersSet = AnswersSet {
  sex ∷ Integer,
  ageRange ∷ Integer,
  country ∷ Integer,
  commune ∷ Maybe Integer
} deriving (Show)

instance FromRow AnswersSet where
  fromRow = AnswersSet <$> field <*> field <*> field <*> field

instance ToRow AnswersSet where
toRow r = [toField (sex r), toField (ageRange r), toField (country r), toField (commune r)]

错误是:

    ‘fromRow’ is not a (visible) method of class ‘FromRow’
   |
17 |   fromRow = AnswersSet <$> field <*> field <*> field <*> field
   |   ^^^^^^^

并且:

    ‘toRow’ is not a (visible) method of class ‘ToRow’
   |
20 |         toRow r = [toField (sex r), toField (ageRange r), toField (country r), toField (commune r)]
   |         ^^^^^

我查看了一些示例项目(其中包括),但我不明白我做错了什么:(

4

1 回答 1

7

该模块Database.PostgreSQL.Simple仅导出类型类ToRowFromRow,而不导出它们的任何方法。

对于这些方法,您需要导入模块Database.PostgreSQL.Simple.ToRowDatabase.PostgreSQL.Simple.FromRow,正如您链接到的示例中所做的那样。

于 2018-03-23T22:41:28.677 回答