Gorm: how to insert null value to datetime column ?

Created on 12 Jun 2018  Â·  2Comments  Â·  Source: go-gorm/gorm

btw: when fetch a row from database, the null will be "0001-01-01 00:00:00", how to make it to be null ?

Most helpful comment

Just make the struct field a pointer to time.Time

type User struct {
gorm.Model
FirstPurchase *time.Time
}

All 2 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

satb picture satb  Â·  3Comments

Quentin-M picture Quentin-M  Â·  3Comments

bramp picture bramp  Â·  3Comments

hypertornado picture hypertornado  Â·  3Comments

youtwo123 picture youtwo123  Â·  3Comments