Hello everyone,
I’m Darshan Hiranandani, working on a project using pgxpool
to query a PostgreSQL database, and I’ve come across an issue when handling queries that may return no rows. Here’s the scenario:
I’m using the query:
SELECT * FROM invoices WHERE user_id = $1, id
This query returns a pgx.Rows
type, which contains the results of the query along with any associated error. The issue is that pgx.Query()
doesn’t return an error for when no rows are found, which leaves me with no way to differentiate between an empty result and an actual error.
I understand that pgx.QueryRow()
combined with pgxscan.ScanOne()
can return a NoRowsError
, but this only works when you’re expecting a single row. In my case, I need to return multiple rows, so this doesn’t fit my use case.
From what I’ve gathered, I should be using Rows.Next()
to check if the next row exists, and if not, it will return false
. The problem is that when rows are returned, I’m unsure of the next steps.
Has anyone encountered this issue and found a good way to handle it while working with multiple rows in pgx
? Any advice or examples would be greatly appreciated.
Thanks in advance!
Regards
Darshan Hiranandani