reflect.Value.Set using unaddressable value

你离开我真会死。 提交于 2019-12-03 14:14:44

You have to call Find with a pointer to the slice.

err := db.Find(&users).Error

relevant Gorm documentation: http://jinzhu.me/gorm/crud.html#query

Just to add clarification to S.Diego answers, changing this:

err := db.Find(users).Error

to this:

err := db.Find(&users).Error

the error says, the variable users is not addressable, because it is not a pointer.

In a very similar fashion to the accepted answer (but in a slightly different context), and an error that I keep making in different projects:

func migrate(db *gorm.DB) {
    db.AutoMigrate(User{}, Activity{})
}

becomes

func migrate(db *gorm.DB) {
    db.AutoMigrate(&User{}, &Activity{})
}

Notice the ampersands.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!