btw: when fetch a row from database, the null will be "0001-01-01 00:00:00", how to make it to be null ?
Just make the struct field a pointer to time.Time
type User struct {
gorm.Model
FirstPurchase *time.Time
}
The same issue, can not insert null to timestamp field
type StateLog struct {
gorm.Model
EquipmentId string
Equipment *Equipment
StateId string
State *State
Enter *time.Time
Leave *time.Time
}
When I insert a row into db by create function, I get blow sql statement
INSERT INTO "smt_ep_state_logs" ("created_at","updated_at","deleted_at","equipment_id","state_id","enter","leave") VALUES ('2019-12-12 09:39:03','2019-12-12 09:39:03',NULL,'FJX01_KX_A','fault','2019-12-12 01:39:02','2019-12-12 09:39:03') RETURNING "smt_ep_state_logs"."id"
the inserted content:
StateLog<ID: 0, EquipmentId: FJX01_KX_A, StateId: fault, Enter: 2019-12-12 01:39:02 +0000 UTC, Leave: <nil>>
deleted_at field insert null as expected
but leave field change to "2019-12-12 01:39:02" automaticly, where it should be null as deleted_at
Most helpful comment
Just make the struct field a pointer to time.Time
type User struct {
gorm.Model
FirstPurchase *time.Time
}