How to use pointer as a model in querying in gorm database

Viewed 25

ENVIRONTMENT

ORM: github.com/jinzhu/gorm v1.9.16
Go: 1.19
Database: Postgresql
OS: macOS Monterey 12.5

I created this service

func (s *service) FindByID(ctx *gin.Context, id string) (*ModelName, error) {
    var model *ModelName

    err := config.DB.Where(`"id" = ?`, id).First(&model).Error

    if nil != err {
        return nil, err
    }

    return model, nil
}

But if I run that code, it will return the error unsupported destination, should be slice or struct. That only happens when I'm using github.com/jinzhu/gorm, if I changed that to gorm.io/gorm, it will work fine.

I still want to use github.com/jinzhu/gorm but I also want to be able to return nil if the there is an error, that can only work if I'm using a pointer.

How can I do that?

0 Answers
Related