From 75ec40de67154d1354e8237c50467cbe0f33a154 Mon Sep 17 00:00:00 2001 From: liaoqiang Date: Thu, 4 Jun 2015 16:39:07 +0800 Subject: [PATCH 1/2] insert_id only support auto_increment field --- model_struct.go | 5 +++++ scope.go | 7 +++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/model_struct.go b/model_struct.go index 10423ae2..4494669d 100644 --- a/model_struct.go +++ b/model_struct.go @@ -40,6 +40,7 @@ type StructField struct { Tag reflect.StructTag Struct reflect.StructField IsForeignKey bool + IsAutoIncrement bool Relationship *Relationship } @@ -148,6 +149,10 @@ func (scope *Scope) GetModelStruct() *ModelStruct { field.HasDefaultValue = true } + if _, ok := sqlSettings["AUTO_INCREMENT"]; ok { + field.IsAutoIncrement = true + } + if value, ok := gormSettings["COLUMN"]; ok { field.DBName = value } else { diff --git a/scope.go b/scope.go index 11bad777..8cf1da3e 100644 --- a/scope.go +++ b/scope.go @@ -112,12 +112,11 @@ func (scope *Scope) HasError() bool { func (scope *Scope) PrimaryField() *Field { if primaryFields := scope.GetModelStruct().PrimaryFields; len(primaryFields) > 0 { - if len(primaryFields) > 1 { - if field, ok := scope.Fields()["id"]; ok { - return field + for i := 0; i < len(primaryFields); i++ { + if primaryFields[i].IsAutoIncrement { + return scope.Fields()[primaryFields[i].DBName] } } - return scope.Fields()[primaryFields[0].DBName] } return nil } From 75c2f14933c785151f9c4d097232d8796498440c Mon Sep 17 00:00:00 2001 From: liaoqiang Date: Mon, 15 Jun 2015 18:29:23 +0800 Subject: [PATCH 2/2] insert_id first support auto_increment field --- callback_create.go | 4 ++-- scope.go | 21 ++++++++++++++++----- structs_test.go | 2 +- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/callback_create.go b/callback_create.go index e7ec40bb..0aca4eeb 100644 --- a/callback_create.go +++ b/callback_create.go @@ -70,8 +70,8 @@ func Create(scope *Scope) { id, err := result.LastInsertId() if scope.Err(err) == nil { scope.db.RowsAffected, _ = result.RowsAffected() - if primaryField != nil && primaryField.IsBlank { - scope.Err(scope.SetColumn(primaryField, id)) + if autoIncrementField := scope.AutoIncrementField(); autoIncrementField != nil { + scope.Err(scope.SetColumn(autoIncrementField, id)) } } } diff --git a/scope.go b/scope.go index 8cf1da3e..a161d33b 100644 --- a/scope.go +++ b/scope.go @@ -109,16 +109,27 @@ func (scope *Scope) Log(v ...interface{}) { func (scope *Scope) HasError() bool { return scope.db.Error != nil } - func (scope *Scope) PrimaryField() *Field { if primaryFields := scope.GetModelStruct().PrimaryFields; len(primaryFields) > 0 { - for i := 0; i < len(primaryFields); i++ { - if primaryFields[i].IsAutoIncrement { - return scope.Fields()[primaryFields[i].DBName] + if len(primaryFields) > 1 { + if field, ok := scope.Fields()["id"]; ok { + return field + } + } + return scope.Fields()[primaryFields[0].DBName] + } + return nil +} + +func (scope *Scope) AutoIncrementField() *Field { + if structFields := scope.GetModelStruct().StructFields; len(structFields) > 0 { + for i := 0; i < len(structFields); i++ { + if structFields[i].IsAutoIncrement { + return scope.Fields()[structFields[i].DBName] } } } - return nil + return scope.PrimaryField() } // PrimaryKey get the primary key's column name diff --git a/structs_test.go b/structs_test.go index 9a9b23d1..45cd5f33 100644 --- a/structs_test.go +++ b/structs_test.go @@ -11,7 +11,7 @@ import ( ) type User struct { - Id int64 + Id int64 `sql:"auto_increment"` Age int64 UserNum Num Name string `sql:"size:255"`