DeletedAt's type has to been *time.Time

This commit is contained in:
Jinzhu 2016-01-10 21:33:15 +08:00
parent f574429f5e
commit 5c57885d98
4 changed files with 11 additions and 8 deletions

View File

@ -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{})

View File

@ -18,7 +18,7 @@ type PersonAddress struct {
gorm.JoinTableHandler
PersonID int
AddressID int
DeletedAt time.Time
DeletedAt *time.Time
CreatedAt time.Time
}

View File

@ -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)
}
}

View File

@ -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 {