1

Here is my query statement to be generated. But I want the offset and limit attributes are optional.

-- name: ListTransfersTo :many

SELECT *
FROM transfers
WHERE to_account_id = $1
ORDER BY created_at
OFFSET $2
LIMIT $3;

I try to do following https://github.com/sqlc-dev/sqlc/issues/1155 to solve this problem by using OFFSET sqlc.arg('offset', nullable => true) and LIMIT sqlc.arg('limit', nullable => true) but it returns an error: expected 1 parameter to sqlc.arg; got 2 after using sqlc generate. Thanks for your supports.

  • [`sqlc.narg`](https://docs.sqlc.dev/en/stable/howto/named_parameters.html#nullable-parameters) is used to indicate a nullable parameter (don't know if this works for `offset`/`limit` but [this](https://github.com/sqlc-dev/sqlc/issues/200#issuecomment-1653328385) may help). – Brits Aug 27 '23 at 06:49
  • It helped me to generate successfully but now the ` ListTransferToParams struct { ... Limit sql.NullInt32, } ` I tried to pass ` ListTransferToParams{ ... Limit: 5, } ` into ListTransfersTo function to query for data, but at Limit key-val, it told me that `cannot use 5 (untyped int constant) as sql.NullInt32 value in struct literal`. How can I fix it? – twentyfourk Aug 27 '23 at 08:55
  • You wanted a nullable parameter; that's represented as a `sql.Null*` so that is what you need to pass (the other option is a pointer). Note: Please don't put code in comments; edit the question (really hare to read in comments and insufficient space to properly describe the issue). – Brits Aug 27 '23 at 09:20

0 Answers0