Deleted

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,

  1. type User struct {
  2. Id int64
  3. Name string
  4. DeletedAt time.Time `xorm:"deleted"`
  5. }

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:

  1. var user User
  2. engine.ID(1).Get(&user)
  3. // SELECT * FROM user WHERE id = ?
  4. engine.ID(1).Delete(&user)
  5. // UPDATE user SET ..., deleted_at = ? WHERE id = ?
  6. engine.ID(1).Get(&user)
  7. // Call Get again, this time it will return false, nil
  8. engine.ID(1).Delete(&user)
  9. // 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:

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