// controller
if total, err := (models.UserAccountMessageRead{}).GetCountByIds(&models.UserAccountMessageRead{
OpenId: openId,
}, msgIds); err == nil && total < int64(msgCount) {
hasMsg = true
}
// model
type UserAccountMessageRead struct {
ID int64 `gorm:"primary_key;type:int(11);unsigned;auto_increment"`
MessageId int64 `json:"message_id"`
OpenId string `json:"open_id"`
DeletedAt gorm.DeletedAt `json:"-" example:"2023-02-03 11:11:11"`
CreatedAt time.Time `json:"created_at" example:"2023-02-03 11:11:11"`
UpdatedAt time.Time `json:"updated_at" example:"2023-02-03 11:11:11"`
}
// query without openid, I have print con.OpenId to make sure it has not empty string
func (uamr UserAccountMessageRead) GetCountByIds(con *UserAccountMessageRead, ids []int64) (total int64, err error) {
err = GoatGameDB.Model(&UserAccountMessageRead{}).Where(&con).Where("message_id in ?", ids).Count(&total).Debug().Error
return
}
// query with openid, I have print con.OpenId to make sure it has not empty string
func (uamr UserAccountMessageRead) GetCountByIds(con *UserAccountMessageRead, ids []int64) (total int64, err error) {
err = GoatGameDB.Model(&UserAccountMessageRead{}).Where(con).Where("message_id in ?", ids).Count(&total).Debug().Error
return
}
Above I was query db by using struct, with or without &
I am not sure why open_id field is missing in the where statement when query with &
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: findmark
My gorm version is 1.20.2
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the
Questiontemplate, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: findmark
nvm, I figure out by going into the source code, In case someone come across same issue, I might just Point it out. Default case won't deel with ptr, only struct, slice and array
// gorm.io/gorm/statement.go
// BuildCondition
reflectValue := reflect.Indirect(reflect.ValueOf(arg))
if s, err := schema.Parse(arg, stmt.DB.cacheStore, stmt.DB.NamingStrategy); err == nil {
switch reflectValue.Kind() {
case reflect.Struct:
for _, field := range s.Fields {
if field.Readable {
if v, isZero := field.ValueOf(reflectValue); !isZero {
if field.DBName != "" {
conds = append(conds, clause.Eq{Column: clause.Column{Table: clause.CurrentTable, Name: field.DBName}, Value: v})
} else if field.DataType != "" {
conds = append(conds, clause.Eq{Column: clause.Column{Table: clause.CurrentTable, Name: field.Name}, Value: v})
}
}
}
}
case reflect.Slice, reflect.Array:
for i := 0; i < reflectValue.Len(); i++ {
for _, field := range s.Fields {
if field.Readable {
if v, isZero := field.ValueOf(reflectValue.Index(i)); !isZero {
if field.DBName != "" {
conds = append(conds, clause.Eq{Column: clause.Column{Table: clause.CurrentTable, Name: field.DBName}, Value: v})
} else if field.DataType != "" {
conds = append(conds, clause.Eq{Column: clause.Column{Table: clause.CurrentTable, Name: field.Name}, Value: v})
}
}
}
}
}
}