Skip to content

Commit e4b130d

Browse files
committed
Fix customize DeletedAt's column name
1 parent 89f6d74 commit e4b130d

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

callback_delete.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ func deleteCallback(scope *Scope) {
3333
extraOption = fmt.Sprint(str)
3434
}
3535

36-
if !scope.Search.Unscoped && scope.HasColumn("DeletedAt") {
36+
deletedAtField, hasDeletedAtField := scope.FieldByName("DeletedAt")
37+
38+
if !scope.Search.Unscoped && hasDeletedAtField {
3739
scope.Raw(fmt.Sprintf(
38-
"UPDATE %v SET deleted_at=%v%v%v",
40+
"UPDATE %v SET %v=%v%v%v",
3941
scope.QuotedTableName(),
42+
scope.Quote(deletedAtField.DBName),
4043
scope.AddToVars(NowFunc()),
4144
addExtraSpaceIfExist(scope.CombinedConditionSql()),
4245
addExtraSpaceIfExist(extraOption),

delete_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,26 @@ func TestSoftDelete(t *testing.T) {
6666
t.Errorf("Can't find permanently deleted record")
6767
}
6868
}
69+
70+
func TestSoftDeleteWithCustomizedDeletedAtColumnName(t *testing.T) {
71+
creditCard := CreditCard{Number: "411111111234567"}
72+
DB.Save(&creditCard)
73+
DB.Delete(&creditCard)
74+
75+
if deletedAtField, ok := DB.NewScope(&CreditCard{}).FieldByName("DeletedAt"); !ok || deletedAtField.DBName != "deleted_time" {
76+
t.Errorf("CreditCard's DeletedAt's column name should be `deleted_time`")
77+
}
78+
79+
if DB.First(&CreditCard{}, "number = ?", creditCard.Number).Error == nil {
80+
t.Errorf("Can't find a soft deleted record")
81+
}
82+
83+
if err := DB.Unscoped().First(&CreditCard{}, "number = ?", creditCard.Number).Error; err != nil {
84+
t.Errorf("Should be able to find soft deleted record with Unscoped, but err=%s", err)
85+
}
86+
87+
DB.Unscoped().Delete(&creditCard)
88+
if !DB.Unscoped().First(&CreditCard{}, "number = ?", creditCard.Number).RecordNotFound() {
89+
t.Errorf("Can't find permanently deleted record")
90+
}
91+
}

migration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ type CreditCard struct {
6666
UserId sql.NullInt64
6767
CreatedAt time.Time `sql:"not null"`
6868
UpdatedAt time.Time
69-
DeletedAt *time.Time
69+
DeletedAt *time.Time `sql:"column:deleted_time"`
7070
}
7171

7272
type Email struct {

scope.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,11 +673,12 @@ func (scope *Scope) buildSelectQuery(clause map[string]interface{}) (str string)
673673
func (scope *Scope) whereSQL() (sql string) {
674674
var (
675675
quotedTableName = scope.QuotedTableName()
676+
deletedAtField, hasDeletedAtField = scope.FieldByName("DeletedAt")
676677
primaryConditions, andConditions, orConditions []string
677678
)
678679

679-
if !scope.Search.Unscoped && scope.HasColumn("deleted_at") {
680-
sql := fmt.Sprintf("%v.deleted_at IS NULL", quotedTableName)
680+
if !scope.Search.Unscoped && hasDeletedAtField {
681+
sql := fmt.Sprintf("%v.%v IS NULL", quotedTableName, scope.Quote(deletedAtField.DBName))
681682
primaryConditions = append(primaryConditions, sql)
682683
}
683684

0 commit comments

Comments
 (0)