Skip to content

A select max(column) returns an interface{} which is useless! #3918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pdenapo opened this issue Apr 3, 2025 · 1 comment
Closed

A select max(column) returns an interface{} which is useless! #3918

pdenapo opened this issue Apr 3, 2025 · 1 comment
Labels
📚 sqlite bug Something isn't working 🔧 golang

Comments

@pdenapo
Copy link

pdenapo commented Apr 3, 2025

Version

1.28.0

What happened?

I have the following query

-- name: Obtener_maximo :one
SELECT max(columna) from tabla;

[ columna in integer primary key in sqlite]

The generated code is like this

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

The problem with this code is that it returns a interface{} that can be of any type, which is useless. Instead, it should return an sql.NullInt64

It is enough to change the declaration of the max variable as

var max sql.NullInt64

A solution would be add an optional type to the query anotation like

-- name: Obtener_maximo :one sql.NullInt64
SELECT max(columna) from tabla;

to be used in the generated code.

@pdenapo pdenapo added the bug Something isn't working label Apr 3, 2025
@pdenapo
Copy link
Author

pdenapo commented Apr 4, 2025

It turns out that you can use the result of the query via a type assertion like

value,_ := Obtener_maximo().(int64)

I didn't realize that. May be it should be better explained in the documentation.

@pdenapo pdenapo closed this as completed Apr 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📚 sqlite bug Something isn't working 🔧 golang
Projects
None yet
Development

No branches or pull requests

1 participant