Your Question
In using the "Find to Map" feature it seems that even when specifying the table name a model is still required.
If I specify a model with one or more fields that map to column(s) in the table, then the Find succeeds.
The map that is returned uses the database column names as keys - even finding columns that I did not specify in my model.
Since GORM seems to be able to get the table metadata to see the column names, and then also successfully gives me back that data in a map using the column names as keys, then why does it fail when I don't provide a model?
Citing the documentation:
// Scanning multiple results into a slice of maps with Table
var results []map[string]interface{}
db.Table("users").Find(&results)
// SQL: SELECT * FROM
users
I do this:
rows := make([]map[string]any, 0, 500)
...
db.Table("table_name").Where(find).FindInBatches(&rows, 500,
func(db gorm.DB, batchID int) error {
// ...
return nil
})
And the result is:
2024/03/14 11:48:38 appl/gorm.go:120 model value required
[0.079ms] [rows:0] SELECT * FROM "table_name" ORDER BY "table_name". LIMIT 500
When I do this, I get back a map of all columns/values:
// There are actually many more columns than just this in the table, but this suffices
// to get things to work!
// Note that I get back all columns in the map, not just created_at
type Blank struct {
CreatedAt time.Time
}
...
db.Model(&Blank{}).Table("table_name").Where(find).FindInBatches(&rows, 500,
func(db gorm.DB, batchID int) error {
// ...
return nil
})
Comment From: ccrenshawSSS
No response after six months. Abandoning all hope.
Comment From: Percivalll
The same question....