database/sql QueryRow Scan panic

422 views
Skip to first unread message

krolaw

unread,
Jan 15, 2014, 9:18:13 PM1/15/14
to golan...@googlegroups.com
Hi,

I have some code that is causing database/sql Scan to panic.   I've created a simple test case that causes the problem:


panic: reflect.Set: value of type string is not assignable to type main.SpecialString

goroutine 1 [running]:
runtime.panic(0x409fa00, 0xc21000a270)
/usr/local/go/src/pkg/runtime/panic.c:266 +0xb6
reflect.Value.assignTo(0x409fa00, 0xc21000a260, 0x182, 0x40e2410, 0xb, ...)
/usr/local/go/src/pkg/reflect/value.go:2198 +0x312
reflect.Value.Set(0x409f940, 0xc21000a210, 0x186, 0x409fa00, 0xc21000a260, ...)
/usr/local/go/src/pkg/reflect/value.go:1385 +0x9b
database/sql.convertAssign(0x4097440, 0xc21000a210, 0x409fa00, 0xc21000a260, 0x401eb71, ...)
/usr/local/go/src/pkg/database/sql/convert.go:215 +0x830
database/sql.(*Rows).Scan(0xc210039120, 0x45b1e78, 0x1, 0x1, 0x0, ...)
/usr/local/go/src/pkg/database/sql/sql.go:1562 +0x2d5
database/sql.(*Row).Scan(0xc2100510a0, 0x45b1e78, 0x1, 0x1, 0x0, ...)
/usr/local/go/src/pkg/database/sql/sql.go:1630 +0x200
main.main()
/Users/krolaw/Dropbox/go/src/websync/sqlfail.go:34 +0x57c

goroutine 4 [syscall]:
runtime.goexit()
/usr/local/go/src/pkg/runtime/proc.c:1394
exit status 2


Since Scan returns an error, if there's a problem, it seems odd that it's panicking, so I'm wondering if this is a bug.

Also, in my real code, the variable passed to Scan provides methods for a specified interface (hence the type based on a string and not a string itself), and it would add extra complexity if I had to unbox such variables for the purpose of Scan.

Thanks in advance.

Tad Glines

unread,
Jan 16, 2014, 12:42:21 PM1/16/14
to krolaw, golang-nuts
The expectation is that if you are going to pass in a custom type to Scan then that custom type should implement the database.Scanner interface. See database.NullString for an example.

As for the panic (instead of returning an error), I think that this might be a bug. In convertAssign, line 214 should probably be "if dv.Type() == sv.Type()" instead of "if dv.Kind() == sv.Kind".

You will probably always be required to implement database.Scanner for all custom types passed to Scan. But the panic might be a bug, please create an issue for it.



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

gwenn...@gmail.com

unread,
Jan 16, 2014, 1:22:18 PM1/16/14
to golan...@googlegroups.com, krolaw
Hello,
I got an error (not a panic) with go tip:
2014/01/16 19:15:26 QueryRow Failure:sql: Scan error on column index 0: unsupported driver -> Scan pair: []uint8 -> *main.SpecialString
Regards.

$ go version
go version devel +ca4b7db0560c Sat Jan 11 20:16:39 2014 +1100 darwin/amd64

Regards.

krolaw

unread,
Jan 16, 2014, 3:50:49 PM1/16/14
to golan...@googlegroups.com, krolaw
Thanks for the Scanner interface tip, my code now works after adding the function.  I'd be happy to create an issue, but it looks like it's already been fixed (as per Gwen's tests) in the latest tip.  Please advise if I should still create an issue.

Thanks again.

palaniappan meiyappan

unread,
Jun 30, 2014, 6:46:08 PM6/30/14
to golan...@googlegroups.com, kro...@gmail.com
Can you please share the scanner interface. I have got an similar error.

Thanks.
Reply all
Reply to author
Forward
0 new messages