框架慢慢修改
This commit is contained in:
@@ -12,7 +12,9 @@ import (
|
||||
"go_blog/pkg/util"
|
||||
"time"
|
||||
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
var DB *gorm.DB
|
||||
@@ -23,20 +25,22 @@ func SetUp(isOrmDebug bool) {
|
||||
conf.DataBase.Password,
|
||||
conf.DataBase.Host,
|
||||
conf.DataBase.Port,
|
||||
conf.DataBase.DB,
|
||||
conf.DataBase.DBName,
|
||||
conf.DataBase.Charset)
|
||||
db, err := gorm.Open(conf.DataBase.Type, conUri)
|
||||
|
||||
db, err := gorm.Open(mysql.Open(conUri), &gorm.Config{
|
||||
NamingStrategy: schema.NamingStrategy{
|
||||
TablePrefix: conf.DataBase.Prefix,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
DB = db
|
||||
DB.LogMode(isOrmDebug)
|
||||
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
|
||||
return conf.DataBase.Prefix + defaultTableName
|
||||
}
|
||||
|
||||
DB.AutoMigrate(&Account{})
|
||||
DB.AutoMigrate(&Content{})
|
||||
DB.AutoMigrate(&Contents{})
|
||||
|
||||
}
|
||||
|
||||
@@ -48,7 +52,7 @@ type BaseModel struct {
|
||||
}
|
||||
|
||||
// 生成全局唯一ID
|
||||
func (m *BaseModel) BeforeCreate(scope *gorm.Scope) error {
|
||||
func (m *BaseModel) BeforeCreate(scope *gorm.DB) error {
|
||||
if m.ID == 0 {
|
||||
m.ID = util.GenSonyFlakeId()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user