title: postgres-simple - No instance for (ToRow Int) arising from a use of ‘query’
body: <p>I am new to haskell and have honestly hard times with it. But it expands my thinking, so here we go. 
  I am trying to run a really simple Webserver that queries a Postgres DB and should return the result as JSON.</p>
  
  <p>The query is absolutely simple:
  "Select id,data from MYTABLE where id = 1"</p>
  
  <p>But the type system of haskell is killing me right now and the final type of my actions doesn't match up.
  I am using <a href="https://hackage.haskell.org/package/Spock" rel="nofollow noreferrer">Spock</a> and <a href="https://hackage.haskell.org/package/postgresql-simple" rel="nofollow noreferrer">PostgreSQL-Simple</a> as a Combo.</p>
  
  <p>Most tutorials are either to simple for what I want to do or to difficult. I am somewhere inbetween and miss a lot of Haskell understanding, a lot of my previous problems I have already solved by simple copy and pasting and got a simple version working.</p>
  
  <p>But as soon as I try to pass on a route variable I am failing.
  Here is my working version. My Database Table is here called "Envelope", The important call is where it says <code>get "json"</code> :</p>
  
  <pre><code>{-# LANGUAGE OverloadedStrings #-}
  {-# LANGUAGE FlexibleInstances #-}
  
  module Main where
  
  import Web.Spock
  import Web.Spock.Config
  import Database.PostgreSQL.Simple
  import Data.Pool
  import Data.Aeson (ToJSON(toJSON), object, (.=),Value)
  import  Database.PostgreSQL.Simple.FromRow
  
  type AppAction a = SpockActionCtx () Connection AppSession AppState a
  
  data AppState = EmptyState
  data AppSession = EmptySession
  
  data Envelope = Envelope { envId :: Int, envData :: Value } deriving Show
  
  instance FromRow Envelope where
    fromRow = Envelope &lt;$&gt; field &lt;*&gt; field
  
  instance ToJSON Envelope where
     toJSON (Envelope envA envB) = object [ "id" .= envA, "data" .= envB ]
  
  main :: IO ()
  main =
    do pool&lt;-createPool (connect (ConnectInfo "localhost" 5432 "" "" "envelopes") ) close 1 10 10
       spockCfg &lt;- defaultSpockCfg EmptySession (PCPool pool) EmptyState
       runSpock 8080 (spock spockCfg app)
  
  app :: SpockM Connection AppSession AppState ()
  app = do 
      get root $
        text "Hello World!"
      get "json" $ do
        xs&lt;-runQuery $ \conn -&gt; 
          query_ conn  "select id,data from envelope where id = 1"
        json (xs::[Envelope])
  </code></pre>
  
  <p>Then I try to pass on the Envelope ID with a lambda function, for that i also need to change PostgreSQL-Simple <code>query_</code> to <code>query</code>:</p>
  
  <pre><code>    get ( "json" &lt;//&gt; var  ) $ \eid -&gt; do
        xs&lt;-runQuery $ \conn -&gt; 
          query conn  "select id,data from envelope where id = ?" (eid :: Int)
        json (xs::[Envelope])
  </code></pre>
  
  <p>The error I get says:</p>
  
  <pre><code> No instance for (ToRow Int) arising from a use of ‘query’
     In the expression:
        query conn "select id,data from envelope where id = ?" (eid :: Int)
      In the second argument of ‘($)’, namely
        ‘\ conn
           -&gt; query
                conn "select id,data from envelope where id = ?" (eid :: Int)’
      In a stmt of a 'do' block:
        xs &lt;- runQuery
              $ \ conn
                  -&gt; query
                       conn "select id,data from envelope where id = ?" (eid :: Int)
  </code></pre>
  
  <p>I also have problem to return just the first item from the query, even without the lambda function.</p>
  
  <p>Full Source can be found on <a href="https://bitbucket.org/bjng/envelopes-server" rel="nofollow noreferrer">bitbucket</a></p>
  
  <p>I hope someone has the time to help me out here.
  Thanks for reading already.</p>