insert_id first support auto_increment field
This commit is contained in:
parent
75ec40de67
commit
75c2f14933
@ -70,8 +70,8 @@ func Create(scope *Scope) {
|
|||||||
id, err := result.LastInsertId()
|
id, err := result.LastInsertId()
|
||||||
if scope.Err(err) == nil {
|
if scope.Err(err) == nil {
|
||||||
scope.db.RowsAffected, _ = result.RowsAffected()
|
scope.db.RowsAffected, _ = result.RowsAffected()
|
||||||
if primaryField != nil && primaryField.IsBlank {
|
if autoIncrementField := scope.AutoIncrementField(); autoIncrementField != nil {
|
||||||
scope.Err(scope.SetColumn(primaryField, id))
|
scope.Err(scope.SetColumn(autoIncrementField, id))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
21
scope.go
21
scope.go
@ -109,16 +109,27 @@ func (scope *Scope) Log(v ...interface{}) {
|
|||||||
func (scope *Scope) HasError() bool {
|
func (scope *Scope) HasError() bool {
|
||||||
return scope.db.Error != nil
|
return scope.db.Error != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) PrimaryField() *Field {
|
func (scope *Scope) PrimaryField() *Field {
|
||||||
if primaryFields := scope.GetModelStruct().PrimaryFields; len(primaryFields) > 0 {
|
if primaryFields := scope.GetModelStruct().PrimaryFields; len(primaryFields) > 0 {
|
||||||
for i := 0; i < len(primaryFields); i++ {
|
if len(primaryFields) > 1 {
|
||||||
if primaryFields[i].IsAutoIncrement {
|
if field, ok := scope.Fields()["id"]; ok {
|
||||||
return scope.Fields()[primaryFields[i].DBName]
|
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
|
// PrimaryKey get the primary key's column name
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
Id int64
|
Id int64 `sql:"auto_increment"`
|
||||||
Age int64
|
Age int64
|
||||||
UserNum Num
|
UserNum Num
|
||||||
Name string `sql:"size:255"`
|
Name string `sql:"size:255"`
|
||||||
|
Loading…
x
Reference in New Issue
Block a user