1

I'm learning Haskell so I decided to write a web app. I did choose PostgreSQL Simple to work with the database. I successfully connected to it and tried simple math operations but I'm having problems trying to map records to data. This code doesn't compile:

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)]

The error is:

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

And also:

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

I looked some sample projects (this among others) but I don't get what I'm doing wrong :(

Tae
  • 1,665
  • 5
  • 24
  • 45
  • Your type is very strange. Why all those `Integer`s? FYI, multiple choice for sex is not likely to work well for mammals. – dfeuer Mar 24 '18 at 01:17

1 Answers1

7

The module Database.PostgreSQL.Simple only exports the type classes ToRow and FromRow, without any of their methods.

For those methods you need to import the modules Database.PostgreSQL.Simple.ToRow and Database.PostgreSQL.Simple.FromRow, as is done in the sample you linked to.

Mor A.
  • 3,805
  • 2
  • 16
  • 19
  • Thanks! I thought `import Database.PostgreSQL.Simple (ToRow)` was the same. I need to read a little bit more about modules – Tae Mar 24 '18 at 15:32
  • 1
    That import only bring the type class into scope(even if the module exported both the class and its methods), and that only exists in the type-level namespace. Haskell has a different namespace for types and values. – Mor A. Mar 24 '18 at 18:41
  • I see! Thank you again! – Tae Mar 24 '18 at 20:41