change the method of initializing slice and fixed the length to be specified as 0
This commit is contained in:
parent
73d44a4f97
commit
16bda61a03
@ -39,10 +39,10 @@ func SaveBeforeAssociations(create bool) func(db *gorm.DB) {
|
||||
switch db.Statement.ReflectValue.Kind() {
|
||||
case reflect.Slice, reflect.Array:
|
||||
var (
|
||||
objs []reflect.Value
|
||||
fieldType = rel.Field.FieldType
|
||||
isPtr = fieldType.Kind() == reflect.Ptr
|
||||
)
|
||||
objs := make([]reflect.Value, 0, db.Statement.ReflectValue.Len())
|
||||
|
||||
if !isPtr {
|
||||
fieldType = reflect.PtrTo(fieldType)
|
||||
@ -140,7 +140,7 @@ func SaveAfterAssociations(create bool) func(db *gorm.DB) {
|
||||
}
|
||||
|
||||
if elems.Len() > 0 {
|
||||
assignmentColumns := []string{}
|
||||
assignmentColumns := make([]string, 0, len(rel.References))
|
||||
for _, ref := range rel.References {
|
||||
assignmentColumns = append(assignmentColumns, ref.ForeignKey.DBName)
|
||||
}
|
||||
@ -154,7 +154,7 @@ func SaveAfterAssociations(create bool) func(db *gorm.DB) {
|
||||
f = f.Addr()
|
||||
}
|
||||
|
||||
assignmentColumns := []string{}
|
||||
assignmentColumns := make([]string, 0, len(rel.References))
|
||||
for _, ref := range rel.References {
|
||||
if ref.OwnPrimaryKey {
|
||||
fv, _ := ref.PrimaryKey.ValueOf(db.Statement.ReflectValue)
|
||||
@ -219,7 +219,7 @@ func SaveAfterAssociations(create bool) func(db *gorm.DB) {
|
||||
}
|
||||
|
||||
if elems.Len() > 0 {
|
||||
assignmentColumns := []string{}
|
||||
assignmentColumns := make([]string, 0, len(rel.References))
|
||||
for _, ref := range rel.References {
|
||||
assignmentColumns = append(assignmentColumns, ref.ForeignKey.DBName)
|
||||
}
|
||||
@ -324,7 +324,7 @@ func onConflictOption(stmt *gorm.Statement, s *schema.Schema, selectColumns map[
|
||||
}
|
||||
|
||||
if len(defaultUpdatingColumns) > 0 {
|
||||
var columns []clause.Column
|
||||
columns := make([]clause.Column, 0, len(s.PrimaryFieldDBNames))
|
||||
for _, dbName := range s.PrimaryFieldDBNames {
|
||||
columns = append(columns, clause.Column{Name: dbName})
|
||||
}
|
||||
@ -340,10 +340,11 @@ func onConflictOption(stmt *gorm.Statement, s *schema.Schema, selectColumns map[
|
||||
|
||||
func saveAssociations(db *gorm.DB, rel *schema.Relationship, values interface{}, selectColumns map[string]bool, restricted bool, defaultUpdatingColumns []string) error {
|
||||
var (
|
||||
selects, omits []string
|
||||
onConflict = onConflictOption(db.Statement, rel.FieldSchema, selectColumns, restricted, defaultUpdatingColumns)
|
||||
refName = rel.Name + "."
|
||||
onConflict = onConflictOption(db.Statement, rel.FieldSchema, selectColumns, restricted, defaultUpdatingColumns)
|
||||
refName = rel.Name + "."
|
||||
)
|
||||
selects := make([]string, 0, len(selectColumns))
|
||||
omits := make([]string, 0, len(selectColumns))
|
||||
|
||||
for name, ok := range selectColumns {
|
||||
columnName := ""
|
||||
|
@ -41,7 +41,7 @@ func DeleteBeforeAssociations(db *gorm.DB) {
|
||||
}
|
||||
|
||||
if len(db.Statement.Selects) > 0 {
|
||||
var selects []string
|
||||
selects := make([]string, 0, len(db.Statement.Selects))
|
||||
for _, s := range db.Statement.Selects {
|
||||
if s == clause.Associations {
|
||||
selects = append(selects, s)
|
||||
@ -69,14 +69,15 @@ func DeleteBeforeAssociations(db *gorm.DB) {
|
||||
}
|
||||
case schema.Many2Many:
|
||||
var (
|
||||
queryConds []clause.Expression
|
||||
foreignFields []*schema.Field
|
||||
relForeignKeys []string
|
||||
modelValue = reflect.New(rel.JoinTable.ModelType).Interface()
|
||||
table = rel.JoinTable.Table
|
||||
tx = db.Session(&gorm.Session{NewDB: true}).Model(modelValue).Table(table)
|
||||
modelValue = reflect.New(rel.JoinTable.ModelType).Interface()
|
||||
table = rel.JoinTable.Table
|
||||
tx = db.Session(&gorm.Session{NewDB: true}).Model(modelValue).Table(table)
|
||||
)
|
||||
|
||||
queryConds := make([]clause.Expression, 0, len(rel.References))
|
||||
foreignFields := make([]*schema.Field, 0, len(rel.References))
|
||||
relForeignKeys := make([]string, 0, len(rel.References))
|
||||
|
||||
for _, ref := range rel.References {
|
||||
if ref.OwnPrimaryKey {
|
||||
foreignFields = append(foreignFields, ref.PrimaryKey)
|
||||
|
@ -27,8 +27,10 @@ func preload(db *gorm.DB, rel *schema.Relationship, conds []interface{}, preload
|
||||
})
|
||||
|
||||
if rel.JoinTable != nil {
|
||||
var joinForeignFields, joinRelForeignFields []*schema.Field
|
||||
var joinForeignKeys []string
|
||||
joinForeignFields := make([]*schema.Field, 0, len(rel.References))
|
||||
joinRelForeignFields := make([]*schema.Field, 0, len(rel.References))
|
||||
joinForeignKeys := make([]string, 0, len(rel.References))
|
||||
|
||||
for _, ref := range rel.References {
|
||||
if ref.OwnPrimaryKey {
|
||||
joinForeignKeys = append(joinForeignKeys, ref.ForeignKey.DBName)
|
||||
|
@ -92,7 +92,7 @@ var (
|
||||
)
|
||||
|
||||
func init() {
|
||||
var commonInitialismsForReplacer []string
|
||||
commonInitialismsForReplacer := make([]string, 0, len(commonInitialisms))
|
||||
for _, initialism := range commonInitialisms {
|
||||
commonInitialismsForReplacer = append(commonInitialismsForReplacer, initialism, strings.Title(strings.ToLower(initialism)))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user