1 回答
TA贡献1854条经验 获得超8个赞
根据自定义类型的gorm,需要实现值和扫描。
您也可以尝试在结构中指定类型(将<TYPE>替换为列类型)。
type Client struct {
ClientID uint64 `gorm:"primaryKey"`
UserID uint8
ClientType ClientType `gorm:"type:<TYPE>"`
CreatedAt time.Time
}
String() 似乎没有问题,因为它不是在创建期间调用的,而是仅在查找期间调用的。
使用 db.LogMode(true),它也在创建期间被调用,但这似乎是用于sql查询日志记录,而不是用于发送到DB。
关闭日志模式
db.LogMode(false)
fmt.Println("Creating")
db.Create(&Client{ClientID: 9, UserID: 8, ClientType: USER, CreatedAt: time.Now()})
fmt.Println("Created")
fmt.Println("Finding")
db.First(&client)
fmt.Printf("Found %v\n", client)
输出
Creating
Created
Finding
String() called with 0
Found {9 8 User 2021-01-28 11:41:51.355633057 +0530 +0530}
开启登录模式
db.LogMode(true)
fmt.Println("Creating")
db.Create(&Client{ClientID: 9, UserID: 8, ClientType: USER, CreatedAt: time.Now()})
fmt.Println("Created")
fmt.Println("Finding")
db.First(&client)
fmt.Printf("Found %v\n", client)
输出:
Creating
String() called with 0
[2021-01-28 11:39:30] [0.95ms] INSERT INTO "clients" ("client_id","user_id","client_type","created_at") VALUES (9,8,'User','2021-01-28 11:39:30')
[1 rows affected or returned ]
Created
Finding
[2021-01-28 11:39:30] [1.49ms] SELECT * FROM "clients" LIMIT 1
[1 rows affected or returned ]
String() called with 0
Found {9 8 User 2021-01-28 11:42:31.245628274 +0530 +0530}
- 1 回答
- 0 关注
- 159 浏览
添加回答
举报