我想在GORM中使用Raw函数作为我个人项目的需求。
这是我的实体
type Student struct {
ID int `json:"id"`
Name string `json:"name"`
Address string `json:"address"`
}因此,我创建常量以在原始参数中被调用。
const (RetrieveData = `SELECT id, name, address FROM users WHERE id = ?`)然后我构建函数
type mysqlRepository struct {
GormDb *gorm.DB
}
func(repo *mysqlRepository) RetrieveUserData(id int) (Student, error) {
data := Student{}
db := db.GormDb.Raw(RetrieveData, 3).Scan(&data)
return data, db.Error
}为什么我会收到关于我的功能的警告
undeclared name: RetrieveData (compile)go-staticcheck是因为不类型的字符串类型吗?
发布于 2022-08-24 10:38:54
很可能RetrieveData在func RetrieveUserData中是不可访问的
要么将其移动到同一个包中,要么使用<package_name>.RetrieveData
发布于 2022-08-24 09:59:44
使用方法find()
err := DB.Raw("[sql]").Find(&data).Errorhttps://stackoverflow.com/questions/73470995
复制相似问题