title: Why am I getting ErrNoRows("sql: no rows in result set") after inserting into postgres, even when insert was actually successful?
body: <p>I am working with golang-postgres:</p>
  
  <pre><code>"database/sql"
  _ "github.com/lib/pq"
  </code></pre>
  
  <p><strong>What I am doing:</strong></p>
  
  <p>I run a select query.
  If I don't get any entry on selecting, I go ahead and insert one.
  Else, update, or something else...</p>
  
  <p><strong>The problem is</strong>, every time insert returns <strong>ErrNoRows("sql: no rows in result set")</strong> , even when I can see in database that INSERT was successful, and row was added.</p>
  
  <p>Also <strong>lastInsertID</strong> always remains 0, while if I check in DB, it has an actual value(say, 131)</p>
  
  <p>It seems as if the error near <em>comment 2</em> below in the code, is cached somewhere, and shows up again.</p>
  
  <pre><code>// 1. Select first
  sQueryStmt, sPrepErr := db.Prepare(selectQuery)
      if sPrepErr != nil {
          glog.V(3).Infof("selectQuery prepare failed. err: %v", sPrepErr)
          return sPrepErr
      }
  
      queryErr := sQueryStmt.QueryRow(today, itemID).Scan(&amp;a, &amp;b, &amp;c, &amp;d, &amp;e, &amp;f, &amp;g, &amp;h, &amp;i, &amp;j)
      if queryErr != nil {
  
  // 2. Getting ErrNoRows here is expected, for the first entry
          if queryErr == sql.ErrNoRows {
              glog.V(3).Infof("selectQuery returned no results. err: %v", queryErr)
  
              var lastInsertID int
              iQueryStmt, iPrepErr := db.Prepare(insertQuery)
              if iPrepErr != nil {
                  glog.V(3).Infof("insertQuery prepare failed. err: %v", iPrepErr)
                  return iPrepErr
              }
              insertErr := iQueryStmt.QueryRow(today, today, today, taskID).Scan(&amp;lastInsertID)
              if insertErr != nil {
                  // 3. Somehow, even after row is correctly being created, I am getting insertErr as "sql: no rows in result set" here
                  glog.V(3).Infof("insertQuery failed. err: %v", insertErr)
              } else {
  // control never comes to this part.
                  glog.V(3).Infof("insertQuery Successful. lastInsertID: %v", lastInsertID)
              }
          } else {
              glog.V(3).Infof("Couldn't run query on Postgres database. err: %v", queryErr)
              return queryErr
          }
  }
  </code></pre>
  
  <p>Debugging inside <strong>database/sql</strong>, shows that it is returning this error in below code block:</p>
  
  <pre><code>    if !r.rows.Next() {
          if err := r.rows.Err(); err != nil {
              return err
          }
          return ErrNoRows
      }
  </code></pre>
  
  <p>But every time, if I check in DB, the <strong>data is always already inserted</strong>.</p>
  
  <p><strong>Edit:</strong></p>
  
  <pre><code>selectQuery = `select task_id, creation_date, times_x_ran,
                          a_count, b_count, c_count, d_count, e_count 
                          from reports where creation_date = $1 and task_id = $2`
  
  insertQuery = `insert into reports 
                  (creation_date, creation_time, last_modified, task_id, times_x_ran, times_y_ran, a, b, c, d, e, x_errors, y_errors) 
                      values 
                      ($1, $2, $3, $4,
                      1, 0, 
                      0, 0, 0, 0, 0, 
                      '', '' )`
  </code></pre>