Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting ErrNoRows("sql: no rows in result set") after inserting into postgres, even when insert was actually successful?

Tags:

postgresql

go

pq

I am working with golang-postgres:

"database/sql"
_ "github.com/lib/pq"

What I am doing:

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...

The problem is, every time insert returns ErrNoRows("sql: no rows in result set") , even when I can see in database that INSERT was successful, and row was added.

Also lastInsertID always remains 0, while if I check in DB, it has an actual value(say, 131)

It seems as if the error near comment 2 below in the code, is cached somewhere, and shows up again.

// 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(&a, &b, &c, &d, &e, &f, &g, &h, &i, &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(&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
        }
}

Debugging inside database/sql, shows that it is returning this error in below code block:

    if !r.rows.Next() {
        if err := r.rows.Err(); err != nil {
            return err
        }
        return ErrNoRows
    }

But every time, if I check in DB, the data is always already inserted.

Edit:

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, 
                    '', '' )`
like image 942
Tushar Avatar asked Nov 27 '18 20:11

Tushar


1 Answers

You've effectively asked two questions, so I'll answer both:

  1. Why ErrNoRows even when your insert is successful?

    This is expected behavior, because by default an INSERT returns no rows. Your "error" isn't actually problematic. If you want an INSERT to return rows, you must use a 'RETURNING' clause at the end.

    The fact that the "No rows in result set" state is communicated by way of an error may be the reason for your confusion. But this should only be considered an error if you expected rows.

  2. Also lastInsertID always remains 0.

    This is also expected behavior. As documented:

    pq does not support the LastInsertId() method of the Result type in database/sql. To return the identifier of an INSERT (or UPDATE or DELETE), use the Postgres RETURNING clause with a standard Query or QueryRow call:

like image 53
Flimzy Avatar answered Nov 15 '22 05:11

Flimzy