sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

NamedStmtContext - prepared statement doesn't support slice of struct?

Open kunwardeep opened this issue 5 years ago • 8 comments

I tried to do this

txn.NamedStmtContext(ctx, namedStatement).ExecContext(ctx, sqlStatement.Args)

Instead of executing I get a panic

panic: reflect: call of github.com/jmoiron/sqlx/reflectx.(*Mapper).TraversalsByNameFunc on slice Value [recovered]
     | 	panic: reflect: call of github.com/jmoiron/sqlx/reflectx.(*Mapper).TraversalsByNameFunc on slice Value
     | goroutine 72 [running]:
     | testing.tRunner.func1(0xc000350200)
     | 	/usr/local/Cellar/go/1.13.3/libexec/src/testing/testing.go:874 +0x69f
     | panic(0x197fba0, 0xc0001a2e80)
     | 	/usr/local/Cellar/go/1.13.3/libexec/src/runtime/panic.go:679 +0x1b2
     | github.com/jmoiron/sqlx/reflectx.mustBe(0x6c76630, 0x193cc60, 0x19)
     | 	/Users/<local_dir>/vendor/github.com/jmoiron/sqlx/reflectx/reflect.go:249 +0xfc
     | github.com/jmoiron/sqlx/reflectx.(*Mapper).TraversalsByNameFunc(0xc0001be1b0, 0x1b7fb40, 0x193cc60, 0xc0001cf7c0, 0x8, 0xa, 0xc000149660, 0x0, 0x1)
     | 	/Users/<local_dir>/vendor/github.com/jmoiron/sqlx/reflectx/reflect.go:187 +0xb3
     | github.com/jmoiron/sqlx.bindArgs(0xc0001cf7c0, 0x8, 0xa, 0x193cc60, 0xc0001a2d20, 0xc0001be1b0, 0x0, 0x2b846d0, 0x0, 0xc0001a2e60, ...)
     | 	/Users/<local_dir>/vendor/github.com/jmoiron/sqlx/named.go:168 +0x20c
     | github.com/jmoiron/sqlx.bindAnyArgs(0xc0001cf7c0, 0x8, 0xa, 0x193cc60, 0xc0001a2d20, 0xc0001be1b0, 0xc0000ca008, 0xc0001c0990, 0xc0001c0ab0, 0x0, ...)
     | 	/Users/<local_dir>/vendor/github.com/jmoiron/sqlx/named.go:153 +0x168
     | github.com/jmoiron/sqlx.(*NamedStmt).ExecContext(0xc000149908, 0x1b70cc0, 0xc0000ca008, 0x193cc60, 0xc0001a2d20, 0xc0001a2e60, 0xc0001a2e40, 0x0, 0x0)
     | 	/Users/<local_dir>/vendor/github.com/jmoiron/sqlx/named_context.go:37 +0x10c
     

To reproduce

args = map[string]interface{}{"id": 1}
deleteQry := delete from table_name where id =: id
deleteStatement, err := sqlx..PrepareNamed(deleteQry)
if err != nil {
	return nil, err
}
txn, errBeginTransaction:= sqlx.BeginTxx(ctx, &sql.TxOptions{Isolation: isolationLevel})
txn.NamedStmtContext(ctx, deleteStatement).ExecContext(ctx, args)

It might be related to this https://github.com/jmoiron/sqlx/issues/519

kunwardeep avatar Dec 03 '19 01:12 kunwardeep