Your Question
我在使用自动创建表的功能时,对所有表的ID属性需要设置自增,我创建了一个根结构体
type Model struct {
ID uint `json:"id" gorm:"primary key;auto_increment;"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
但是当表创建完毕后,检查发现自增并未设置,我进行了Debug发现无论我是否设置ID属性的primary_key,他都将作为主键生效,并且标识了为自增:
当我查看DataTypeOf函数时,我发现他会拒绝primaryKey为true的field进行拼接autoincrement
我有些困惑,请问我该怎么才能为ID属性设置自增呢?
type Model struct {
ID uint `json:"id" gorm:"type:integer primary key autoincrement"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
目前我是采取的这种方式,完全替换掉由框架生成的type。
The document you expected this should be explained
Expected answer
Comment From: duanshanghanqing
require ( gorm.io/driver/mysql v1.4.7 gorm.io/gorm v1.24.6 )
require ( github.com/go-sql-driver/mysql v1.7.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect )
你用这个版本,我和你遇到刚好相反的问题,不设置 autoincrement , 反而总是自动设置。
Comment From: redbirdztc
@marisa-4219
自增应该设置为autoIncrement
auto_increment should be claimed as autoIncrement.
@duanshanghanqing I got the same issue.