From b616d810eb43678ec37d078b1ffb633416003764 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 13 Jul 2021 19:29:10 +0800 Subject: [PATCH] Fix scan single value to custom type, close #4501 --- scan.go | 2 ++ tests/scan_test.go | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/scan.go b/scan.go index e82e3f07..c4f88cf8 100644 --- a/scan.go +++ b/scan.go @@ -238,6 +238,8 @@ func Scan(rows *sql.Rows, db *DB, initialized bool) { } } } + default: + db.AddError(rows.Scan(dest)) } } diff --git a/tests/scan_test.go b/tests/scan_test.go index 86cb0399..67d5f385 100644 --- a/tests/scan_test.go +++ b/tests/scan_test.go @@ -63,6 +63,13 @@ func TestScan(t *testing.T) { if len(results) != 2 || results[0].Name != user2.Name || results[1].Name != user3.Name { t.Errorf("Scan into struct map, got %#v", results) } + + type ID uint64 + var id ID + DB.Raw("select id from users where id = ?", user2.ID).Scan(&id) + if uint(id) != user2.ID { + t.Errorf("Failed to scan to customized data type") + } } func TestScanRows(t *testing.T) {