Your Question

I an using this code. The generated sql is SELECT * FROM `modules` WHERE status="1" ORDER BY `modules`.`status` LIMIT 1

package db_test

import (
    "testing"

    "gorm.io/driver/sqlite"
    "gorm.io/gorm"
)

type StatusEnum int64

const (
    StatusEnumEnable StatusEnum = iota
    StatusEnumDisable
)

var StatusEnumMap = map[StatusEnum]string{
    StatusEnumEnable:  "Enable",
    StatusEnumDisable: "Disable",
}

//func (e StatusEnum) String() string {
//  if v, ok := StatusEnumMap[e]; ok {
//      return v
//  }
//  return "unknown"
//}

type module struct {
    Status StatusEnum
}

func TestEnum(t *testing.T) {
    db, _ := gorm.Open(sqlite.Open(":memory:"))

    db.AutoMigrate(&module{})

    db.Model(&module{}).Create(&module{Status: StatusEnumDisable})

    var out *module
    // SELECT * FROM `modules` WHERE status=1 ORDER BY `modules`.`status` LIMIT 1
    db.Debug().Model(&module{}).Where("status=?", StatusEnumDisable).First(&out)

    t.Log(out)
    //t.Logf("status value: %d, status string: %s", out.Status, out.Status)
}

The document you expected this should be explained

Expected answer

The generate sql condition should be status=1 instead of status="1".

Comment From: a631807682

You can use Scanner / Valuer

func (s StatusEnum) Value() (driver.Value, error) {
    return int64(s), nil
}

Comment From: GGXXLL

@a631807682 Thanks for your quick response. But in mysql general_log, the execute sql is status=1. The debug log info is mislead. Gorm Correct conversion of type aliases

If the field has an index, the log message will mislead the user into thinking that the index will not be used(The right side of the equation has the wrong type).

Comment From: a631807682

What gorm prints is not the real log. In fact, the log can only be obtained in the driver. Gorm cannot obtain the actual execution sql log. For example, the acquisition of the value depends on the database/sql/convert.go , but as a log, I think it is not necessary.