mirror of https://github.com/go-gorm/gorm.git
Fix customize DeletedAt's column name
This commit is contained in:
parent
89f6d74b5e
commit
e4b130d2d7
|
@ -33,10 +33,13 @@ func deleteCallback(scope *Scope) {
|
|||
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(
|
||||
"UPDATE %v SET deleted_at=%v%v%v",
|
||||
"UPDATE %v SET %v=%v%v%v",
|
||||
scope.QuotedTableName(),
|
||||
scope.Quote(deletedAtField.DBName),
|
||||
scope.AddToVars(NowFunc()),
|
||||
addExtraSpaceIfExist(scope.CombinedConditionSql()),
|
||||
addExtraSpaceIfExist(extraOption),
|
||||
|
|
|
@ -66,3 +66,26 @@ func TestSoftDelete(t *testing.T) {
|
|||
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")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -66,7 +66,7 @@ type CreditCard struct {
|
|||
UserId sql.NullInt64
|
||||
CreatedAt time.Time `sql:"not null"`
|
||||
UpdatedAt time.Time
|
||||
DeletedAt *time.Time
|
||||
DeletedAt *time.Time `sql:"column:deleted_time"`
|
||||
}
|
||||
|
||||
type Email struct {
|
||||
|
|
5
scope.go
5
scope.go
|
@ -673,11 +673,12 @@ func (scope *Scope) buildSelectQuery(clause map[string]interface{}) (str string)
|
|||
func (scope *Scope) whereSQL() (sql string) {
|
||||
var (
|
||||
quotedTableName = scope.QuotedTableName()
|
||||
deletedAtField, hasDeletedAtField = scope.FieldByName("DeletedAt")
|
||||
primaryConditions, andConditions, orConditions []string
|
||||
)
|
||||
|
||||
if !scope.Search.Unscoped && scope.HasColumn("deleted_at") {
|
||||
sql := fmt.Sprintf("%v.deleted_at IS NULL", quotedTableName)
|
||||
if !scope.Search.Unscoped && hasDeletedAtField {
|
||||
sql := fmt.Sprintf("%v.%v IS NULL", quotedTableName, scope.Quote(deletedAtField.DBName))
|
||||
primaryConditions = append(primaryConditions, sql)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue