Skip to content
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

Ugly error when trying to select into a single struct #2

Closed
natefinch opened this issue Jun 24, 2013 · 1 comment
Closed

Ugly error when trying to select into a single struct #2

natefinch opened this issue Jun 24, 2013 · 1 comment

Comments

@natefinch
Copy link

So, I misunderstood the API of Select, and didn't realize it was always expecting a slice... and so I passed it a pointer to a struct, figuring it would realize it was a struct and just do one row.... I got back a really ugly panic:

reflect.(_rtype).Elem(...)
/home/nate/go/src/pkg/reflect/type.go:584 +0x118
github.com/jmoiron/sqlx.StructScan(...)
/home/nate/code/go/src/github.com/jmoiron/sqlx/sqlx.go:761 +0x1b4
github.com/jmoiron/sqlx.Select(...)
/home/nate/code/go/src/github.com/jmoiron/sqlx/sqlx.go:508 +0xbe
github.com/jmoiron/sqlx.(_DB).Select(...)
/home/nate/code/go/src/github.com/jmoiron/sqlx/sqlx.go:160 +0x71

There seem to be a couple ways to handle this with better results:

1.) Allow this to work, just check to see if the thing passed in is a struct, and if so, just scan the first returned row
2.) Don't allow it to work, but handle the error with a more friendly error message

for #2, it might also be beneficial to rename the parameter as something like dest_slice ... to make it a little more obvious that it requires a slice.

@jmoiron
Copy link
Owner

jmoiron commented Jun 24, 2013

This should definitely not panic but return a proper err instead. There is already dbx.Get() which is the QueryRow of Select's Query, so doubling that behavior in Select will probably be more of a bug than a feature for most people.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants