diff --git a/delete_test.go b/delete_test.go index e0c71660..d3de0a6d 100644 --- a/delete_test.go +++ b/delete_test.go @@ -45,7 +45,7 @@ func TestSoftDelete(t *testing.T) { type User struct { Id int64 Name string - DeletedAt time.Time + DeletedAt *time.Time } DB.AutoMigrate(&User{}) diff --git a/join_table_test.go b/join_table_test.go index 3353aee2..ce92e42f 100644 --- a/join_table_test.go +++ b/join_table_test.go @@ -18,7 +18,7 @@ type PersonAddress struct { gorm.JoinTableHandler PersonID int AddressID int - DeletedAt time.Time + DeletedAt *time.Time CreatedAt time.Time } diff --git a/scope_private.go b/scope_private.go index d893c095..f8a68229 100644 --- a/scope_private.go +++ b/scope_private.go @@ -159,16 +159,19 @@ func (scope *Scope) buildSelectQuery(clause map[string]interface{}) (str string) } func (scope *Scope) whereSql() (sql string) { - var primaryConditions, andConditions, orConditions []string + var ( + quotedTableName = scope.QuotedTableName() + primaryConditions, andConditions, orConditions []string + ) - if !scope.Search.Unscoped && scope.Fields()["deleted_at"] != nil { - sql := fmt.Sprintf("(%v.deleted_at IS NULL OR %v.deleted_at <= '0001-01-02')", scope.QuotedTableName(), scope.QuotedTableName()) + if !scope.Search.Unscoped && scope.HasColumn("deleted_at") { + sql := fmt.Sprintf("%v.deleted_at IS NULL", quotedTableName) primaryConditions = append(primaryConditions, sql) } if !scope.PrimaryKeyZero() { for _, field := range scope.PrimaryFields() { - sql := fmt.Sprintf("(%v = %v)", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface())) + sql := fmt.Sprintf("%v.%v = %v", quotedTableName, scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface())) primaryConditions = append(primaryConditions, sql) } } diff --git a/structs_test.go b/structs_test.go index a3dfa8b1..ef04cd4b 100644 --- a/structs_test.go +++ b/structs_test.go @@ -42,7 +42,7 @@ type CreditCard struct { UserId sql.NullInt64 CreatedAt time.Time UpdatedAt time.Time - DeletedAt time.Time + DeletedAt *time.Time } type Email struct { @@ -60,7 +60,7 @@ type Address struct { Post string CreatedAt time.Time UpdatedAt time.Time - DeletedAt time.Time + DeletedAt *time.Time } type Language struct {