Skip to content
This repository was archived by the owner on May 24, 2020. It is now read-only.

Latest commit

 

History

History
35 lines (29 loc) · 1.05 KB

1.deleted.md

File metadata and controls

35 lines (29 loc) · 1.05 KB

Deleted

Deleted will not really remove one record from table but only tag as deleted via current time. This feature ask you use xorm:"deleted", The field type could be time.Time, type MyTime time.Time or int, int64. For example,

type User struct {
    Id int64
    Name string
    DeletedAt time.Time `xorm:"deleted"`
}

When Delete() will be called, tag deleted will automatically be filled as current time and this record will not be deleted in fact. For example:

var user User
engine.Id(1).Get(&user)
// SELECT * FROM user WHERE id = ?
engine.Id(1).Delete(&user)
// UPDATE user SET ..., deleted_at = ? WHERE id = ?
engine.Id(1).Get(&user)
// Call Get again, this time it will return false, nil
engine.Id(1).Delete(&user)
// Call delete again, it will return 0, nil

But how to get this record if you really need retrieve it? You need Unscoped, for example:

var user User
engine.Id(1).Unscoped().Get(&user)
// Then you will return true, nil
engine.Id(1).Unscoped().Delete(&user)
// Then this record will be really deleted