As per gorm for custom types Value and Scan need to be implemented.
You can try specifying type in struct (replace <TYPE> with column type) too.
type Client struct {
ClientID uint64 `gorm:"primaryKey"`
UserID uint8
ClientType ClientType `gorm:"type:<TYPE>"`
CreatedAt time.Time
}
String() does not seems to be issue as it is not called during creates, but only during finds.
With db.LogMode(true)
, it is called during create also, but that seems to be for sql query logging and not for sending to DB.
With Log Mode off
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
", client)
output
Creating
Created
Finding
String() called with 0
Found {9 8 User 2021-01-28 11:41:51.355633057 +0530 +0530}
With Log Mode on
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
", client)
Output:
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}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…