Following is the struct of the Genre Model :
type Genre struct {
ImageId int
GenreType string
}
I am executing the following query for finding all the genreTypes for an ImageId. The code is as follows:
func getGenresOfImage(DB *gorm.DB, w http.ResponseWriter, imageId int) ([]string, error) {
var genres []string
var allGenres []models.Genre
genresOfCurrentImage := DB.Where("ImageId = ?", strconv.Itoa(imageId)).Find(&allGenres)
genreRows, err := genresOfCurrentImage.Rows()
if err != nil {
SendErrorResponse(w, http.StatusInternalServerError, err.Error())
// error is returned here
return genres, err
}
I am getting the following error:
Images.go:46 no such column: ImageId
[0.908ms] [rows:0] SELECT * FROM `genres` WHERE ImageId = "1"
Is the syntax that I am using wrong for finding the all the genres associated with an imageId? Any help is appreciated!
Most likely your column name will be image_id
.
As the gorm documentation says:
Column db name uses the field’s name’s snake_case by convention.
The tables get generated this way when you use gorm's auto migrate feature.
For further info see: https://gorm.io/docs/conventions.html#Column-Name