Merge 75c2f14933c785151f9c4d097232d8796498440c into d35a44c5b123c3a44c95beeff5d0f1f8bce6ed3b

This commit is contained in:
Liao Qiang 2015-06-24 06:18:18 +00:00
commit 6d4e086686
4 changed files with 19 additions and 4 deletions

View File

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

View File

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

View File

@ -109,7 +109,6 @@ 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 {
if len(primaryFields) > 1 {
@ -122,6 +121,17 @@ func (scope *Scope) PrimaryField() *Field {
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 scope.PrimaryField()
}
// PrimaryKey get the primary key's column name
func (scope *Scope) PrimaryKey() string {
if field := scope.PrimaryField(); field != nil {

View File

@ -11,7 +11,7 @@ import (
)
type User struct {
Id int64
Id int64 `sql:"auto_increment"`
Age int64
UserNum Num
Name string `sql:"size:255"`