修改生成ID插件出错
This commit is contained in:
parent
ca7619af41
commit
05f17b9c73
|
@ -1,10 +1,11 @@
|
||||||
package identity
|
package identity
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
|
|
||||||
"github.com/rs/xid"
|
"github.com/rs/xid"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"gorm.io/gorm/schema"
|
"gorm.io/gorm/schema"
|
||||||
"reflect"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Identify struct {
|
type Identify struct {
|
||||||
|
@ -31,10 +32,13 @@ func (identity *Identify) Grant(db *gorm.DB) {
|
||||||
if db.Statement.Schema == nil {
|
if db.Statement.Schema == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if field = db.Statement.Schema.LookUpField("ID"); field == nil {
|
for _, column := range db.Statement.Schema.Fields {
|
||||||
return
|
if column.PrimaryKey {
|
||||||
|
field = column
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if field.DataType != schema.String {
|
if field == nil || field.DataType != schema.String {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if db.Statement.ReflectValue.Kind() == reflect.Array || db.Statement.ReflectValue.Kind() == reflect.Slice {
|
if db.Statement.ReflectValue.Kind() == reflect.Array || db.Statement.ReflectValue.Kind() == reflect.Slice {
|
||||||
|
@ -47,7 +51,7 @@ func (identity *Identify) Grant(db *gorm.DB) {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if _, zero := field.ValueOf(db.Statement.Context, db.Statement.ReflectValue); zero {
|
if _, zero := field.ValueOf(db.Statement.Context, db.Statement.ReflectValue); zero {
|
||||||
db.Statement.SetColumn("ID", identity.NextID())
|
db.Statement.SetColumn(field.Name, identity.NextID())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue