Skip to content

[sqlx] does not support struct fields with lowercase (unexported) names. #4745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jiangf0775 opened this issue Mar 30, 2025 · 1 comment
Open

Comments

@jiangf0775
Copy link

Describe the bug
sqlx不支持结构体小写的变量名

To Reproduce
Steps to reproduce the behavior, if applicable:

  1. The code is

func main() {
DbQuery()
}

func NewMysql() sqlx.SqlConn {
mysql := sqlx.NewMysql("xxx")
db, err := mysql.RawDB()
if err != nil {
panic(err)
}

ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
defer cancel()

err = db.PingContext(ctx)
if err != nil {
	panic(err)
}
return mysql

}
func DbQuery() {
mysql := NewMysql()
var resp []BaseOption
ctx, cancel := context.WithTimeout(context.Background(), time.Second
60)
defer cancel()

query := "select * from base_option where deleted = ?"
var values = []interface{}{0}

err := mysql.QueryRowsCtx(ctx, &resp, query, values...)
if err != nil {
	fmt.Println(err)
	return
}
for _, v := range resp {
	// fmt.Println(v)
	fmt.Printf("%+v \n", v)
}

}
type (
BaseModel struct {
Id uint64 db:"id"
Deleted uint64 db:"deleted"
CreatedDate time.Time db:"created_date"
CreatedUserName string db:"created_user_name"
CreatedUserId uint64 db:"created_user_id"
ModifiedDate sql.NullTime db:"modified_date"
ModifiedUserName sql.NullString db:"modified_user_name"
ModifiedUserId sql.NullInt64 db:"modified_user_id"
}
BaseOption struct {
//数据库表映射的结构体
Name string db:"name"
Value string db:"value"
description sql.NullString db:"description"//****小写变量名
BaseModel db:"-"
}
)

func (m *BaseOption) GetDesc() string {
return m.description.String
}
func (m *BaseOption) SetDesc() {

}


2. The error is

value not addressable or interfaceable


**Expected behavior**
A clear and concise description of what you expected to happen.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Environments (please complete the following information):**
- OS: [e.g. Windows]
- go-zero version [e.g. v1.7.4]
- goctl version [e.g. 1.21.6]

**More description**

@kevwan kevwan changed the title 【sqlx】不支持结构体的小写变量名 [sqlx] does not support struct fields with lowercase (unexported) names. Mar 30, 2025
@hthuz
Copy link
Contributor

hthuz commented Apr 1, 2025

Underlying QueryRowsCtx, reflect.Value.Interface() is used to parse the fields of struct. But unexported fields can't be accessed with reflect, possible for safety reason. Actually, if you json.Marshal/Unmarshal a struct, unexported fields won't be marshaled/unmarshaled.
So I believe the simplest solution is to use uppercase names. Or use an intermediate struct with all uppercase names for query if you need lowercase names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants