gorm框架表名自动加s问题


查看日志会发现表名自动加了s
在这里插入图片描述
在model实现以下方法即可解决

type UsUser struct {
	ID            int64     `gorm:"column:id" db:"column:id" json:"id" form:"id"`
	CreatedTime   time.Time `gorm:"column:created_time" db:"column:created_time" json:"created_time" form:"created_time"`
	UpdatedTime   time.Time `gorm:"column:updated_time" db:"column:updated_time" json:"updated_time" form:"updated_time"`
	Username      string    `gorm:"column:username" db:"column:username" json:"username" form:"username"`
	Password      string    `gorm:"column:password" db:"column:password" json:"password" form:"password"`
}

// TableName 解决gorm表明映射
func (UsUser) TableName() string {
	return "us_user"
}

或者在GORM配置中设置

db, err := gorm.Open(mysql.Open(xxx.xxx.xxx.xxx)), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true, // 使用单数表名
		},
	})

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM