Description
i am using Scan to map ids to []int array. But it always populate null values but the row count is coming out to be right.
var ids []int
result := DB.Raw("SELECT id FROM users").Scan(&ids);
fmt.Println(ids) // [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
Not sharing Playground link since if can be fixed by discussion
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: li-jin-gou
hello @vdvibhu20 I run a demo and it is normal.
func TestGORM(t *testing.T) {
for i := 0; i < 10; i++ {
user := User{Name: "jinzhu"}
user.ID = uint(i)
DB.Create(&user)
}
var ids []int64
if err := DB.Model(&User{}).Select("id").Where("name = ?", "jinzhu").Find(&ids).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}
t.Log(ids)
var ids2 []int64
if err := DB.Raw("select id from users where name = ?", "jinzhu").Scan(&ids2).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}
t.Log(ids2)
}
result:
2022/01/31 19:08:27 /Users/hale/go/src/github.com/longlihale/playground/main_test.go:20
[0.094ms] [rows:9] SELECT `id` FROM `users` WHERE name = "jinzhu" AND `users`.`deleted_at` IS NULL
main_test.go:23: [1 2 3 4 5 6 7 8 9]
2022/01/31 19:08:27 /Users/hale/go/src/github.com/longlihale/playground/main_test.go:26
[0.060ms] [rows:9] select id from users where name = "jinzhu"
main_test.go:30: [1 2 3 4 5 6 7 8 9]
--- PASS: TestGORM (0.01s)
hope provide a demo in https://github.com/go-gorm/playground
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨