user error (Pattern match failure in do expression) with PostgreSQL.Simple

256 Views Asked by At

My schema:

CREATE TABLE IF NOT EXISTS feed (
  id SERIAL PRIMARY KEY CHECK (id > 0),
  name TEXT NOT NULL,
  url TEXT NOT NULL
);

My code:

{-# LANGUAGE OverloadedStrings #-}

import           Database.PostgreSQL.Simple

hello :: IO String
hello = do
  conn <- connectPostgreSQL ""
  [Only i] <- query_ conn "SELECT url FROM feed WHERE id = 1"
  return i

main :: IO ()
main = hello >>= print

My error:

user error (Pattern match failure in do expression at src/Main.hs:8:3-10)

My questions:

  • How do I fix this error?
  • How could I get a more informative error message?

Update: I connected to the wrong database and therefor my assumption about the result set was wrong. It contained zero lines instead of one line and thus it tried to match [] against [Only i]. I found it out by executing the query in ghci:

:set -XOverloadedStrings
import Database.PostgreSQL.Simple
do;  conn <- connectPostgreSQL "" ;query_ conn "SELECT url FROM feed WHERE id = 1"
0

There are 0 best solutions below