postgresql-simple hanging query - Haskell

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

James King

I have a record and a FromRow instance for it but when I try to evaluate an action using that query in the REPL it just seems to hang. I've tried everything I can think of but maybe I'm missing something? The code, simplified:

-- MyModuleA.hs
data MyRow
  = MyRow
  { _myRowRowId :: UUID
  , _myRowOtherId :: UUID
  , _myRowDescription :: Text
  deriving (Eq, Generic, Show)

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

-- MyModuleB.hs
getMyRow :: Connection -> MyRowId -> IO (Maybe MyRow)
getMyRow conn rid = do
  r <- query conn
    [sql| select id, other_id, description from my_row where id = ? |] [rid]
  case r of
    [] -> pure $ Nothing
    (r' : _) -> pure $ Just r'

But in the REPL...

λ\=> r <- getMyRow conn rid

Just hangs. I'm able to execute other similar IO actions from the REPL, but not this one for some reason. I've tried various incantations of the parameter list, annotations, etc... it's weird. The query works fine in raw SQL.

James King

Hm... it may have to do with one of the fields having a JSON column and the parseJSON failing.

James King

I did narrow this down to the FromRow instance for my type. It can return a value of type Value but it doesn't try to parse that if I try to give the value a type that has a FromJSON instance. Some reason this causes fromRow to hang.

James King

So the lesson here is to get your row and deserialize your JSON values in a separate computation.