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