'Go: How to pass jsonb argument in a PG query?
I wrote a PG query (a function) which takes in an argument (jsonb).
This works when I run the query through pgAdmin:
SELECT * FROM f_get_profiles(
'[{"role": 1, "experience": 1 }]'::jsonb,
array[2]::int[],
array[1]::int[],
10, -- limit
0 -- skip
)
And this returns me 3 records from the DB.
But I am unable to do the same using go code:
q := `
SELECT * FROM f_get_profiles(
$1::jsonb, $2::int[], $3::int[], $4, $5
)
`
rows, pgErr := p.db.Query(q, f.Expertise, pq.Array(f.Languages), pq.Array(excluded), limit, skip)
if pgErr != nil {
return nil, logAndReturnError(pgErr, "Unable to get profiles using the filters")
}
The go types of arguments passed are:
f.Expertise : json.RawMessage
f.Languages : pq.Int32Array
I wonder what I am doing wrong. I am not getting any array but this is returning empty result.
More details on how I am accessing the rows:
var profiles dto.ProfileSuggestions = dto.ProfileSuggestions{}
for rows.Next() {
var p dto.ProfileSuggestionsNode
err := rows.Scan(&p.UserId, &p.Username, &p.Photo, &p.About, &p.Languages, &p.Expertise)
if err!=nil{
return nil, logAndReturnError(err, "Unable to scan the profile")
}
profiles = append(profiles, p)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
