MCPcopy
hub / github.com/go-gorm/gorm / saveAssociations

Function saveAssociations

callbacks/associations.go:378–432  ·  view source on GitHub ↗
(db *gorm.DB, rel *schema.Relationship, rValues reflect.Value, selectColumns map[string]bool, restricted bool, defaultUpdatingColumns []string)

Source from the content-addressed store, hash-verified

376}
377
378func saveAssociations(db *gorm.DB, rel *schema.Relationship, rValues reflect.Value, selectColumns map[string]bool, restricted bool, defaultUpdatingColumns []string) error {
379 // stop save association loop
380 if checkAssociationsSaved(db, rValues) {
381 return nil
382 }
383
384 var (
385 selects, omits []string
386 onConflict = onConflictOption(db.Statement, rel.FieldSchema, defaultUpdatingColumns)
387 refName = rel.Name + "."
388 values = rValues.Interface()
389 )
390
391 for name, ok := range selectColumns {
392 columnName := ""
393 if strings.HasPrefix(name, refName) {
394 columnName = strings.TrimPrefix(name, refName)
395 }
396
397 if columnName != "" {
398 if ok {
399 selects = append(selects, columnName)
400 } else {
401 omits = append(omits, columnName)
402 }
403 }
404 }
405
406 tx := db.Session(&gorm.Session{NewDB: true}).Clauses(onConflict).Session(&gorm.Session{
407 FullSaveAssociations: db.FullSaveAssociations,
408 SkipHooks: db.Statement.SkipHooks,
409 DisableNestedTransaction: true,
410 })
411
412 db.Statement.Settings.Range(func(k, v interface{}) bool {
413 tx.Statement.Settings.Store(k, v)
414 return true
415 })
416
417 if tx.Statement.FullSaveAssociations {
418 tx = tx.Set("gorm:update_track_time", true)
419 }
420
421 if len(selects) > 0 {
422 tx = tx.Select(selects)
423 } else if restricted && len(omits) == 0 {
424 tx = tx.Omit(clause.Associations)
425 }
426
427 if len(omits) > 0 {
428 tx = tx.Omit(omits...)
429 }
430
431 return db.AddError(tx.Create(values).Error)
432}
433
434// check association values has been saved
435// if values kind is Struct, check it has been saved

Callers 2

SaveBeforeAssociationsFunction · 0.85
SaveAfterAssociationsFunction · 0.85

Calls 9

checkAssociationsSavedFunction · 0.85
onConflictOptionFunction · 0.85
SessionMethod · 0.80
ClausesMethod · 0.80
SetMethod · 0.65
SelectMethod · 0.65
OmitMethod · 0.65
AddErrorMethod · 0.65
CreateMethod · 0.65

Tested by

no test coverage detected