修改生成ID插件出错

This commit is contained in:
Yavolte 2025-08-06 10:47:45 +08:00
parent ca7619af41
commit 05f17b9c73
1 changed files with 9 additions and 5 deletions

View File

@ -1,10 +1,11 @@
package identity
import (
"reflect"
"github.com/rs/xid"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"reflect"
)
type Identify struct {
@ -31,10 +32,13 @@ func (identity *Identify) Grant(db *gorm.DB) {
if db.Statement.Schema == nil {
return
}
if field = db.Statement.Schema.LookUpField("ID"); field == nil {
return
for _, column := range db.Statement.Schema.Fields {
if column.PrimaryKey {
field = column
break
}
}
if field.DataType != schema.String {
if field == nil || field.DataType != schema.String {
return
}
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 {
if _, zero := field.ValueOf(db.Statement.Context, db.Statement.ReflectValue); zero {
db.Statement.SetColumn("ID", identity.NextID())
db.Statement.SetColumn(field.Name, identity.NextID())
}
}
}