File tree Expand file tree Collapse file tree 4 files changed +32
-5
lines changed
Expand file tree Collapse file tree 4 files changed +32
-5
lines changed Original file line number Diff line number Diff 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 ),
Original file line number Diff line number Diff 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+ }
Original file line number Diff line number Diff 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
7272type Email struct {
Original file line number Diff line number Diff line change @@ -673,11 +673,12 @@ func (scope *Scope) buildSelectQuery(clause map[string]interface{}) (str string)
673673func (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
You can’t perform that action at this time.
0 commit comments