Skip to content

Commit

Permalink
DeletedAt's type has to been *time.Time
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jan 10, 2016
1 parent f574429 commit 5c57885
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})

Expand Down
2 changes: 1 addition & 1 deletion join_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type PersonAddress struct {
gorm.JoinTableHandler
PersonID int
AddressID int
DeletedAt time.Time
DeletedAt *time.Time
CreatedAt time.Time
}

Expand Down
11 changes: 7 additions & 4 deletions scope_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
4 changes: 2 additions & 2 deletions structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -60,7 +60,7 @@ type Address struct {
Post string
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt time.Time
DeletedAt *time.Time
}

type Language struct {
Expand Down

0 comments on commit 5c57885

Please sign in to comment.