43 lines
916 B
Go
43 lines
916 B
Go
![]() |
package model
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"gorm.io/driver/mysql"
|
||
|
"gorm.io/gorm"
|
||
|
"gorm.io/gorm/schema"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
var db *gorm.DB
|
||
|
var err error
|
||
|
|
||
|
func InitDb() {
|
||
|
var datetimePrecision = 2
|
||
|
|
||
|
db, err = gorm.Open(mysql.New(mysql.Config{
|
||
|
DSN: "r:r@tcp(0.0.0.0:0)/r?charset=utf8mb4&parseTime=True&loc=Local",
|
||
|
DefaultStringSize: 256,
|
||
|
DisableDatetimePrecision: true,
|
||
|
DefaultDatetimePrecision: &datetimePrecision,
|
||
|
DontSupportRenameIndex: true,
|
||
|
DontSupportRenameColumn: true,
|
||
|
SkipInitializeWithVersion: false,
|
||
|
|
||
|
}), &gorm.Config{
|
||
|
SkipDefaultTransaction: true,
|
||
|
NamingStrategy: schema.NamingStrategy{
|
||
|
SingularTable: true,
|
||
|
},
|
||
|
NowFunc: func() time.Time {
|
||
|
return time.Now().Local()
|
||
|
},
|
||
|
DryRun: false,
|
||
|
PrepareStmt: false,
|
||
|
DisableAutomaticPing: true,
|
||
|
DisableForeignKeyConstraintWhenMigrating: true,
|
||
|
})
|
||
|
if err != nil {
|
||
|
fmt.Println("connect err: ", err)
|
||
|
}
|
||
|
db.AutoMigrate(&User{})
|
||
|
}
|