lpsmith / postgresql-simple

Mid-level client library for accessing PostgreSQL from Haskell
Other
206 stars 71 forks source link

partial result ends in incompatible types error #251

Closed rsoeldner closed 6 years ago

rsoeldner commented 6 years ago

When selecting partial results from the database i receive

*** Exception: Incompatible {errSQLType = "record", errSQLTableOid = Nothing, errSQLField = "row", errHaskellType = "Text", errMessage = "types incompatible"}

My table looks like:

create table Bla (
  timestamp int,
  id varchar(16),
  name varchar(16),
  tmp1 double precision,
  primary key (timestamp, id));

My corresponding type:

  data Bla = Bla {
     bId :: !Text,
     bName :: !Text,
     tmp1 :: Double
     };

I created an FromRow instance :

instance (FromRow Bla) where
  fromRow = Bla
            <$> field -- pId
            <*> field -- bName
            <*> field -- tmp1

But when trying to select them:

getBlas :: Connection -> IO [Bla]
getBlas conn = query_ conn stmt
  where
    stmt :: Query
    stmt = "SELECT (id, name, tmp) FROM Bla"

Thanks again for the help :+1:

cimmanon commented 6 years ago

PostgreSQL Simple doesn't currently support record types, which is what you get when you put parentheses around a list of columns like that. Just remove them and it should work as expected.

getBlas :: Connection -> IO [Bla]
getBlas conn = query_ conn stmt
  where
    stmt :: Query
    stmt = "SELECT id, name, tmp FROM Bla"
rsoeldner commented 6 years ago

@cimmanon thank you verry much :+1: