mirror of https://github.com/go-gorm/gorm.git
callback create.go
This commit is contained in:
parent
8dd7b4ed91
commit
7b8e91377b
|
@ -15,15 +15,17 @@ func Create(scope *Scope) {
|
|||
defer scope.Trace(time.Now())
|
||||
|
||||
if !scope.HasError() {
|
||||
scope.SetColumn("CreatedAt", time.Now())
|
||||
scope.SetColumn("UpdatedAt", time.Now())
|
||||
|
||||
// set create sql
|
||||
var sqls, columns []string
|
||||
|
||||
for _, field := range scope.Fields() {
|
||||
if field.IsBlank || len(field.SqlTag) == 0 {
|
||||
continue
|
||||
if field.DBName != scope.PrimaryKey() && len(field.SqlTag) > 0 && !field.IsIgnored {
|
||||
columns = append(columns, scope.quote(field.DBName))
|
||||
sqls = append(sqls, scope.AddToVars(field.Value))
|
||||
}
|
||||
columns = append(columns, scope.quote(field.DBName))
|
||||
sqls = append(sqls, scope.AddToVars(field.Value))
|
||||
}
|
||||
|
||||
scope.Raw(fmt.Sprintf(
|
||||
|
|
|
@ -22,10 +22,10 @@ type IgnoredEmbedStruct struct {
|
|||
}
|
||||
|
||||
type User struct {
|
||||
Id int64 // Id: Primary key
|
||||
Birthday time.Time // Time
|
||||
Id int64 // Id: Primary key
|
||||
Age int64
|
||||
Name string `sql:"size:255"`
|
||||
Birthday time.Time // Time
|
||||
CreatedAt time.Time // CreatedAt: Time of record is created, will be insert automatically
|
||||
UpdatedAt time.Time // UpdatedAt: Time of record is updated, will be updated automatically
|
||||
DeletedAt time.Time // DeletedAt: Time of record is deleted, refer Soft Delete for more
|
||||
|
@ -189,6 +189,7 @@ func TestPrecision(t *testing.T) {
|
|||
|
||||
var u User
|
||||
db.First(&u, "name = ?", "Precision")
|
||||
|
||||
if u.Latitude != f {
|
||||
t.Errorf("Float64 should not be changed after query")
|
||||
}
|
||||
|
@ -868,6 +869,7 @@ func TestUpdate(t *testing.T) {
|
|||
product1 := Product{Code: "123"}
|
||||
product2 := Product{Code: "234"}
|
||||
db.Save(&product1).Save(&product2).Update("code", "456")
|
||||
|
||||
if product2.Code != "456" {
|
||||
t.Errorf("Record should be updated with update attributes")
|
||||
}
|
||||
|
|
3
main.go
3
main.go
|
@ -116,7 +116,6 @@ func (s *DB) First(out interface{}, where ...interface{}) *DB {
|
|||
func (s *DB) Last(out interface{}, where ...interface{}) *DB {
|
||||
return s.clone().do(out).where(where...).last().db
|
||||
}
|
||||
|
||||
func (s *DB) Find(out interface{}, where ...interface{}) *DB {
|
||||
return s.clone().do(out).where(where...).query().db
|
||||
}
|
||||
|
@ -180,7 +179,7 @@ func (s *DB) UpdateColumns(values interface{}, ignore_protected_attrs ...bool) *
|
|||
func (s *DB) Save(value interface{}) *DB {
|
||||
scope := s.clone().newScope(value)
|
||||
if scope.PrimaryKeyZero() {
|
||||
return scope.callCallbacks(s.parent.callback.creates).db
|
||||
return scope.callCallbacks(s.parent.callback.creates).db.do(value).db
|
||||
} else {
|
||||
return s.clone().do(value).begin().save().commit_or_rollback().db
|
||||
}
|
||||
|
|
14
private.go
14
private.go
|
@ -32,15 +32,15 @@ func (s *DB) do(data interface{}) *Do {
|
|||
|
||||
func (s *DB) err(err error) error {
|
||||
if err != nil {
|
||||
if s.logMode == 0 {
|
||||
if err != RecordNotFound {
|
||||
if err != RecordNotFound {
|
||||
if s.logMode == 0 {
|
||||
go s.print(fileWithLineNum(), err)
|
||||
if regexp.MustCompile(`^sql: Scan error on column index`).MatchString(err.Error()) {
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
s.log(err)
|
||||
}
|
||||
if regexp.MustCompile(`^sql: Scan error on column index`).MatchString(err.Error()) {
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
s.log(err)
|
||||
}
|
||||
s.Error = err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue