'toRow'不是“ToRow”类

时间:2018-03-23 22:28:54

标签: haskell postgresql-simple

我正在学习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)]
   |         ^^^^^

我查看了一些示例项目(this among others),但我不知道我做错了什么:(

1 个答案:

答案 0 :(得分:6)

模块Database.PostgreSQL.Simple仅导出类型类ToRowFromRow,而不使用任何方法。

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