How to get optional output in golang sqlc

172 Views Asked by At

I have table which has column with name next_timestamp. And I want next incoming timestamp. For this I wrote below query

-- name: GetNextTimestamp :one
SELECT MIN(next_timestamp) FROM tableA
WHERE next_timestamp > CURRENT_TIMESTAMP;

When it generated function, it looked like below

func (q *Queries) GetNextTimestamp (ctx context.Context) (interface{}, error) {
    row := q.db.QueryRowContext(ctx, getNextTimestamp )
    var min interface{}
    err := row.Scan(&min)
    return min, err
}

It is returning interface{}, I was expecting sql.NullTime

I thought changing a sql query like below. but sometime next_timestamp > CURRENT_TIMESTAMP this condition may not satisfy for any row which giving error sql: Scan error on column index 0, name "min": unsupported Scan, storing driver.Value type <nil> into type *time.Time

SELECT MIN(next_timestamp)::timestamptz FROM tableA
WHERE next_timestamp > CURRENT_TIMESTAMP;

How can I achieve this?

0

There are 0 best solutions below