Error:
scannable dest type slice with >1 columns (5) in result
Caused in code:
func main() {
// Gets db instance (internal logic)
db := database.GetInstance()
defer db.Close()
items := []dishes.Dish{}
err := All(&items, db)
}
// Get all items
func All(dest *[]Dish, db *sqlx.DB) error {
query, _, _ := sq.Select("*").From(Table).ToSql()
return db.Get(dest, query) // Select * from dishes;
}
Dish (struct):
type Dish struct {
Id int `db:"id" json:"id"`
Label string `db:"label" json:"label" validate:"required"`
Description string `db:"description" json:"description,omitempty"`
Type int `db:"type" json:"type"`
PhotoUrl string `db:"photo_url" json:"photoUrl"`
}
Source SQL table
CREATE TABLE `dishes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`label` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`description` varchar(128) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`type` int(1) NOT NULL DEFAULT '0',
`photo_url` varchar(128) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `dish_id_UNIQUE` (`id`),
UNIQUE KEY `label_UNIQUE` (`label`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
P.S - At that time, only one row was present at the dishes table and i was able to get it by using Dish{} instead of []Dish{} struct. Adding a second item didn't solve this issue.
Use db.Select() instead of db.Get() when fetching multiple records
I meet the same problem when I used a **dest(pointer to pointer to struct) by accident.
Thanks @EndlessCheng, your alternative cause for this error applied to me. I fixed my error using your post.
I experienced the same issue when all of the struct fields we not exported (start with a lowercase letter)
Most helpful comment
Use
db.Select()instead ofdb.Get()when fetching multiple records