I want to make sure my query to find models by ID is correct as I will be using this same pattern for all my structs/models.
func (dbs *DbService) GetUserLocationId(locationId int) (User, error) {
var model User
if dbs.deps.db.Where("location_id = ?", locationId).Find(&model).RecordNotFound() {
return model, errors.New("User not found")
}
return model, nil
}
So a common usecase in a web application is to lookup a model, if it does not exist that I will insert a new record.
using the above I will do:
user, err := GetUserLocationById(123)
if err != nil {
err := InsertNewUser(user)
}
Now what thing I am struggling with is this. If the errors is not nil, then I should insert a new user. But what if the error is because my query inside the function GetUserLocationById has the wrong column name? I dont' want to start inserting rows when they really do exist.
Looking for some advice to make sure I am doing this correctly.