When using using the Soft Delete function with the following structure:
type Reminder struct {
Id int64 `json:"id"`
Message string `gorm:"size:1024" json:"message"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt time.Time 'json:"-"`
}
The statement:
error := i.DB.First(&reminder, id).Error
Returns record not found error. I sourced the problem to the generated sqlite/sql statement:
SELECT * FROM "reminders" WHERE "reminders"."deleted_at" IS NULL AND (("reminders"."id" = '8')) ORDER BY "reminders"."id" ASC LIMIT 1
Specifically, WHERE "reminders"."deleted_at" IS NULL is incorrect as the default value of time.Time is 0001-01-01 00:00:00+00:00 not NULL.
Therefore this must be corrected in the API code, or by putting in the documentation that the soft delete function struct has to add the gorm ignore field parameter to its definition like so:
type Reminder struct {
Id int64 `json:"id"`
Message string `gorm:"size:1024" json:"message"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt time.Time `gorm:"-" json:"-"`
}
DeletedAt's type should be *time.Time
Is that documented somewhere? I changed it to *time.Time and it worked, thank you.
Most helpful comment
DeletedAt's type should be
*time.Time