Fix customize DeletedAt's column name

This commit is contained in:
Jinzhu 2017-02-01 21:33:36 +08:00
parent 89f6d74b5e
commit e4b130d2d7
4 changed files with 32 additions and 5 deletions

View File

@ -33,10 +33,13 @@ func deleteCallback(scope *Scope) {
extraOption = fmt.Sprint(str) extraOption = fmt.Sprint(str)
} }
if !scope.Search.Unscoped && scope.HasColumn("DeletedAt") { deletedAtField, hasDeletedAtField := scope.FieldByName("DeletedAt")
if !scope.Search.Unscoped && hasDeletedAtField {
scope.Raw(fmt.Sprintf( scope.Raw(fmt.Sprintf(
"UPDATE %v SET deleted_at=%v%v%v", "UPDATE %v SET %v=%v%v%v",
scope.QuotedTableName(), scope.QuotedTableName(),
scope.Quote(deletedAtField.DBName),
scope.AddToVars(NowFunc()), scope.AddToVars(NowFunc()),
addExtraSpaceIfExist(scope.CombinedConditionSql()), addExtraSpaceIfExist(scope.CombinedConditionSql()),
addExtraSpaceIfExist(extraOption), addExtraSpaceIfExist(extraOption),

View File

@ -66,3 +66,26 @@ func TestSoftDelete(t *testing.T) {
t.Errorf("Can't find permanently deleted record") t.Errorf("Can't find permanently deleted record")
} }
} }
func TestSoftDeleteWithCustomizedDeletedAtColumnName(t *testing.T) {
creditCard := CreditCard{Number: "411111111234567"}
DB.Save(&creditCard)
DB.Delete(&creditCard)
if deletedAtField, ok := DB.NewScope(&CreditCard{}).FieldByName("DeletedAt"); !ok || deletedAtField.DBName != "deleted_time" {
t.Errorf("CreditCard's DeletedAt's column name should be `deleted_time`")
}
if DB.First(&CreditCard{}, "number = ?", creditCard.Number).Error == nil {
t.Errorf("Can't find a soft deleted record")
}
if err := DB.Unscoped().First(&CreditCard{}, "number = ?", creditCard.Number).Error; err != nil {
t.Errorf("Should be able to find soft deleted record with Unscoped, but err=%s", err)
}
DB.Unscoped().Delete(&creditCard)
if !DB.Unscoped().First(&CreditCard{}, "number = ?", creditCard.Number).RecordNotFound() {
t.Errorf("Can't find permanently deleted record")
}
}

View File

@ -66,7 +66,7 @@ type CreditCard struct {
UserId sql.NullInt64 UserId sql.NullInt64
CreatedAt time.Time `sql:"not null"` CreatedAt time.Time `sql:"not null"`
UpdatedAt time.Time UpdatedAt time.Time
DeletedAt *time.Time DeletedAt *time.Time `sql:"column:deleted_time"`
} }
type Email struct { type Email struct {

View File

@ -673,11 +673,12 @@ func (scope *Scope) buildSelectQuery(clause map[string]interface{}) (str string)
func (scope *Scope) whereSQL() (sql string) { func (scope *Scope) whereSQL() (sql string) {
var ( var (
quotedTableName = scope.QuotedTableName() quotedTableName = scope.QuotedTableName()
deletedAtField, hasDeletedAtField = scope.FieldByName("DeletedAt")
primaryConditions, andConditions, orConditions []string primaryConditions, andConditions, orConditions []string
) )
if !scope.Search.Unscoped && scope.HasColumn("deleted_at") { if !scope.Search.Unscoped && hasDeletedAtField {
sql := fmt.Sprintf("%v.deleted_at IS NULL", quotedTableName) sql := fmt.Sprintf("%v.%v IS NULL", quotedTableName, scope.Quote(deletedAtField.DBName))
primaryConditions = append(primaryConditions, sql) primaryConditions = append(primaryConditions, sql)
} }