insert_id only support auto_increment field
This commit is contained in:
parent
0b8c9f29a9
commit
75ec40de67
@ -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 {
|
||||
|
7
scope.go
7
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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user