Gorm: Soft Delete Query Generation Incorrect

Created on 16 Feb 2017  路  2Comments  路  Source: go-gorm/gorm

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:"-"`
}

Most helpful comment

DeletedAt's type should be *time.Time

All 2 comments

DeletedAt's type should be *time.Time

Is that documented somewhere? I changed it to *time.Time and it worked, thank you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fieryorc picture fieryorc  路  3Comments

leebrooks0 picture leebrooks0  路  3Comments

izouxv picture izouxv  路  3Comments

sredxny picture sredxny  路  3Comments

zeropool picture zeropool  路  3Comments