lib / pq

Pure Go Postgres driver for database/sql
https://pkg.go.dev/github.com/lib/pq
MIT License
8.87k stars 908 forks source link

pq syntax error #758

Open psankar opened 6 years ago

psankar commented 6 years ago

I have the following postgresql query which works fine in psql but fails in the pq driver.

SELECT ifsc, ba.name, branch, address, city, district, state
  FROM branches, banks ba
 WHERE ba.id = bank_id AND ifsc = 'ABHY0065002'

The corresponding golang code is:

err = db.QueryRow(`
SELECT ifsc, ba.name, branch, address, city, district, state
  FROM branches, banks ba
 WHERE ba.id = bank_id AND ifsc = $1`, x.IFSC).Scan(&t.IFSC,
        &t.BankName, &t.Branch, &t.Address, &t.City, &t.District, &t.State)

the error message is: pq: syntax error at or near "branches"

Instead of the $1, I have tried hard-coding the actual IFSC code too (copy pasting the same query from the psql cli) and still I get the same error.

Is it a bug or am I doing something wrong ?

psankar commented 6 years ago

I compressed the SELECT query to a single line (instead of the newlines) and that seem to solve the issue.

db.QueryRow(
        "SELECT ifsc, ba.name, branch, address, city, district, state FROM branches, banks ba WHERE ba.id = bank_id AND ifsc = $1",
        x.IFSC).Scan(&t.IFSC, &t.BankName, &t.Branch,
        &t.Address, &t.City, &t.District, &t.State)