I’m facing the error unexpected Parse response 'C'
and searching on the internet I found out this error happens when rows
are not closed after the previous query.
But in my case I’m only using QueryRow
which return a *sql.Row
where Close
isn’t implemented.
type Queryable interface {
QueryRow(query string, args ...interface{}) *sql.Row
}
func GetOrCreateArticle(q Queryable, article *entity.Article) (id int64, err error) {
const selectQuery string = "SELECT id FROM articles WHERE link = $1"
const insertQuery string = "INSERT INTO articles (link, title) VALUES ($1, $2) RETURNING id"
err = q.QueryRow(selectQuery, article.Link).Scan(&id)
fmt.Println("Error :", err, article.Link)
if err == sql.ErrNoRows {
return id, q.QueryRow(insertQuery, article.Link, article.Title).Scan(&id)
}
return id, err
}
Queryable
is an interface to represent either a *sql.DB
or *sql.Tx
, here the function is always call with a Transaction.
In the same transaction I’m querying / inserting multiple Articles, the first call to this function always ends with the expeted result while the second always throw the error : pq : unexpected Parse response 'C'
.