lib / pq

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

pq: syntax error at or near "$2" #977

Open aditya1808 opened 4 years ago

aditya1808 commented 4 years ago

im executing a where query using gromdb db = db.Where("parameters ? = ?", key, values[0]) key - string, values[0] -> string, parameters is json type example of key: "->key1" the whole query may look like this *select from table where parameters ->>key1 = value**

But im getting this error pq: syntax error at or near "$2" although i didnt specify any $2 in my query. Does it internally modify '?' to $x placeholders?

I tried with different placeholder db = db.Where("parameters $1 = $2", key, values[0]) Then i'm getting pq: syntax error at or near "$1"

could someone help me understand what exactly im doing wrong.

cbandy commented 4 years ago

pq: syntax error at or near "$2"

That is the shape of PostgreSQL query parameters, yes. Those parameters can only take the place of SQL literals (strings or numbers). They cannot replace SQL operators, identifiers, etc.

Does it internally modify '?' to $x placeholders?

lib/pq doesn't know or do anything with ?, so you should probably seek advice from the package that provides db.Where().