go-xorm / xorm

Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle, Moved to https://gitea.com/xorm/xorm
BSD 3-Clause "New" or "Revised" License
6.66k stars 757 forks source link

Field's names contain underscore between a letter and a number? #837

Open p581581 opened 6 years ago

p581581 commented 6 years ago

When field's names contain a underscore between a letter and a number cause "無效的資料行名稱" error, but it works fine when a underscore between letters

For example:

Table Mark
- mark_1 VARCHAR(1)
- mark_2 VARCHAR(1)
- mark_a VARCHAR(1)

then, use xorm reverse to generate Mark.go

type Mark struct {
        Mark1       string  `xorm:"VARCHAR(1)"`
    Mark2       string  `xorm:"VARCHAR(1)"`
    MarkA       string  `xorm:"VARCHAR(1)"`
}

and then use find in my handler:

var marks Mark[]
err := engine.find(&marks)

it throws an error "無效的資料行名稱" Mark2.

please, help.

lunny commented 6 years ago

Because mark_1 -> Mark1 but Mark1 -> mark1.

p581581 commented 6 years ago

yeah I know the rule, but is there another way to fix it and keep underscores, like field's name mapping?

lunny commented 6 years ago

You can write yourself mapper or you can give a specify tag as a column name.

type Mark struct {
        Mark1       string  `xorm:"VARCHAR(1) mark_1"`
    Mark2       string  `xorm:"VARCHAR(1) mark_2"`
    MarkA       string  `xorm:"VARCHAR(1) mark_a"`
}