Savanni D'Gerinel
Savanni D'Gerinel

Reputation: 2489

Using postgres-simple, how do I get multiple parameters from a row

I am trying to make a fairly basic query with postgresql-simple 0.5.0.1 and with 0.5.1.0. I am already familiar with the content in postgresql-simple query error, but I get a different error message.

data SensorRow = SensorRow Int String String deriving Show
instance FromRow SensorRow where
    fromRow = SensorRow <$> field <*> field <*> field

*DatabaseIntegrationSpec> db <- connectPostgreSQL dbString 
*DatabaseIntegrationSpec> r <- query_ db "SELECT (id, name, token) FROM sensor" :: IO [SensorRow]
*** Exception: Incompatible {errSQLType = "record", errSQLTableOid = Nothing, errSQLField = "row", errHaskellType = "Int", errMessage = "types incompatible"}
*DatabaseIntegrationSpec> r <- query_ db "SELECT (id, name, token) FROM sensor" :: IO [(Int, String, String)]
*** Exception: Incompatible {errSQLType = "record", errSQLTableOid = Nothing, errSQLField = "row", errHaskellType = "Int", errMessage = "types incompatible"}
*DatabaseIntegrationSpec> 

By my reading of the documentation and all of the example code, both of these queries should work. However, reading the error message, especially errSQLField, it appears in both cases that the library is trying to convert the entire row into an int.

So, what is wrong here? Have I encountered a bug in the postgresql-simple library, and if so, how can I work around it?

Upvotes: 1

Views: 354

Answers (1)

Savanni D&#39;Gerinel
Savanni D&#39;Gerinel

Reputation: 2489

The entire problem ultimately lay in my SQL query. The correct query to use is:

*DatabaseIntegrationSpec> r <- query_ db "SELECT id, name, token FROM sensor" :: IO [SensorRow]

In short, putting parenthesis around the field names in the query causes PostgreSQL to return things in a format I do not know how to decode.

Upvotes: 3

Related Questions