forked from mirror/gorm
Fix update with customized time type, close #5101
This commit is contained in:
parent
7837fb6fa0
commit
b1201fce4e
|
@ -232,10 +232,10 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) {
|
||||||
set = append(set, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: now.UnixNano()})
|
set = append(set, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: now.UnixNano()})
|
||||||
} else if field.AutoUpdateTime == schema.UnixMillisecond {
|
} else if field.AutoUpdateTime == schema.UnixMillisecond {
|
||||||
set = append(set, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: now.UnixNano() / 1e6})
|
set = append(set, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: now.UnixNano() / 1e6})
|
||||||
} else if field.GORMDataType == schema.Time {
|
} else if field.AutoUpdateTime == schema.UnixSecond {
|
||||||
set = append(set, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: now})
|
|
||||||
} else {
|
|
||||||
set = append(set, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: now.Unix()})
|
set = append(set, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: now.Unix()})
|
||||||
|
} else {
|
||||||
|
set = append(set, clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: now})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -264,10 +264,10 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) {
|
||||||
value = stmt.DB.NowFunc().UnixNano()
|
value = stmt.DB.NowFunc().UnixNano()
|
||||||
} else if field.AutoUpdateTime == schema.UnixMillisecond {
|
} else if field.AutoUpdateTime == schema.UnixMillisecond {
|
||||||
value = stmt.DB.NowFunc().UnixNano() / 1e6
|
value = stmt.DB.NowFunc().UnixNano() / 1e6
|
||||||
} else if field.GORMDataType == schema.Time {
|
} else if field.AutoUpdateTime == schema.UnixSecond {
|
||||||
value = stmt.DB.NowFunc()
|
|
||||||
} else {
|
|
||||||
value = stmt.DB.NowFunc().Unix()
|
value = stmt.DB.NowFunc().Unix()
|
||||||
|
} else {
|
||||||
|
value = stmt.DB.NowFunc()
|
||||||
}
|
}
|
||||||
isZero = false
|
isZero = false
|
||||||
}
|
}
|
||||||
|
|
|
@ -293,6 +293,10 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if field.GORMDataType == "" {
|
||||||
|
field.GORMDataType = field.DataType
|
||||||
|
}
|
||||||
|
|
||||||
if val, ok := field.TagSettings["TYPE"]; ok {
|
if val, ok := field.TagSettings["TYPE"]; ok {
|
||||||
switch DataType(strings.ToLower(val)) {
|
switch DataType(strings.ToLower(val)) {
|
||||||
case Bool, Int, Uint, Float, String, Time, Bytes:
|
case Bool, Int, Uint, Float, String, Time, Bytes:
|
||||||
|
@ -302,10 +306,6 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if field.GORMDataType == "" {
|
|
||||||
field.GORMDataType = field.DataType
|
|
||||||
}
|
|
||||||
|
|
||||||
if field.Size == 0 {
|
if field.Size == 0 {
|
||||||
switch reflect.Indirect(fieldValue).Kind() {
|
switch reflect.Indirect(fieldValue).Kind() {
|
||||||
case reflect.Int, reflect.Int64, reflect.Uint, reflect.Uint64, reflect.Float64:
|
case reflect.Int, reflect.Int64, reflect.Uint, reflect.Uint64, reflect.Float64:
|
||||||
|
|
|
@ -9,11 +9,11 @@ require (
|
||||||
github.com/lib/pq v1.10.4
|
github.com/lib/pq v1.10.4
|
||||||
github.com/mattn/go-sqlite3 v1.14.11 // indirect
|
github.com/mattn/go-sqlite3 v1.14.11 // indirect
|
||||||
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
|
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
|
||||||
gorm.io/driver/mysql v1.3.1
|
gorm.io/driver/mysql v1.3.2
|
||||||
gorm.io/driver/postgres v1.3.1
|
gorm.io/driver/postgres v1.3.1
|
||||||
gorm.io/driver/sqlite v1.3.1
|
gorm.io/driver/sqlite v1.3.1
|
||||||
gorm.io/driver/sqlserver v1.3.1
|
gorm.io/driver/sqlserver v1.3.1
|
||||||
gorm.io/gorm v1.23.0
|
gorm.io/gorm v1.23.1
|
||||||
)
|
)
|
||||||
|
|
||||||
replace gorm.io/gorm => ../
|
replace gorm.io/gorm => ../
|
||||||
|
|
|
@ -2,6 +2,7 @@ package tests_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/lib/pq"
|
"github.com/lib/pq"
|
||||||
|
@ -17,6 +18,8 @@ func TestPostgres(t *testing.T) {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
Name string `gorm:"check:name_checker,name <> ''"`
|
Name string `gorm:"check:name_checker,name <> ''"`
|
||||||
Test uuid.UUID `gorm:"type:uuid;not null;default:gen_random_uuid()"`
|
Test uuid.UUID `gorm:"type:uuid;not null;default:gen_random_uuid()"`
|
||||||
|
CreatedAt time.Time `gorm:"type:TIMESTAMP WITHOUT TIME ZONE"`
|
||||||
|
UpdatedAt time.Time `gorm:"type:TIMESTAMP WITHOUT TIME ZONE"`
|
||||||
Things pq.StringArray `gorm:"type:text[]"`
|
Things pq.StringArray `gorm:"type:text[]"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,6 +51,15 @@ func TestPostgres(t *testing.T) {
|
||||||
if err := DB.Where("id = $1", harumph.ID).First(&Harumph{}).Error; err != nil || harumph.Name != "jinzhu" {
|
if err := DB.Where("id = $1", harumph.ID).First(&Harumph{}).Error; err != nil || harumph.Name != "jinzhu" {
|
||||||
t.Errorf("No error should happen, but got %v", err)
|
t.Errorf("No error should happen, but got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
harumph.Name = "jinzhu1"
|
||||||
|
if err := DB.Save(&harumph).Error; err != nil {
|
||||||
|
t.Errorf("Failed to update date, got error %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := DB.First(&result, "id = ?", harumph.ID).Error; err != nil || harumph.Name != "jinzhu1" {
|
||||||
|
t.Errorf("No error should happen, but got %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Post struct {
|
type Post struct {
|
||||||
|
|
Loading…
Reference in New Issue