Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions callbacks/preload.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ func preloadEntryPoint(db *gorm.DB, joins []string, relationships *schema.Relati
}

tx := preloadDB(db, reflectValue, reflectValue.Interface())
if err := preloadEntryPoint(tx, nestedJoins, &tx.Statement.Schema.Relationships, preloadMap[name], associationsConds); err != nil {
if err := preloadEntryPoint(tx, nestedJoins, tx.Statement.Schema.Relationships, preloadMap[name], associationsConds); err != nil {
return err
}
}
case reflect.Struct, reflect.Pointer:
reflectValue := rel.Field.ReflectValueOf(db.Statement.Context, rv)
tx := preloadDB(db, reflectValue, reflectValue.Interface())
if err := preloadEntryPoint(tx, nestedJoins, &tx.Statement.Schema.Relationships, preloadMap[name], associationsConds); err != nil {
if err := preloadEntryPoint(tx, nestedJoins, tx.Statement.Schema.Relationships, preloadMap[name], associationsConds); err != nil {
return err
}
default:
Expand Down
2 changes: 1 addition & 1 deletion callbacks/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@
return
}

db.AddError(preloadEntryPoint(tx, joins, &tx.Statement.Schema.Relationships, db.Statement.Preloads, db.Statement.Preloads[clause.Associations]))
db.AddError(preloadEntryPoint(tx, joins, tx.Statement.Schema.Relationships, db.Statement.Preloads, db.Statement.Preloads[clause.Associations]))

Check failure on line 294 in callbacks/query.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `db.AddError` is not checked (errcheck)
}
}

Expand Down
2 changes: 1 addition & 1 deletion schema/relationship.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (schema *Schema) setRelation(relation *Relationship) {
if len(relation.Field.EmbeddedBindNames) <= 1 {
return
}
relationships := &schema.Relationships
relationships := schema.Relationships
for i, name := range relation.Field.EmbeddedBindNames {
if i < len(relation.Field.EmbeddedBindNames)-1 {
if relationships.EmbeddedRelations == nil {
Expand Down
10 changes: 8 additions & 2 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Schema struct {
FieldsByBindName map[string]*Field // embedded fields is 'Embed.Field'
FieldsByDBName map[string]*Field
FieldsWithDefaultDBValue []*Field // fields with default value assigned by database
Relationships Relationships
Relationships *Relationships
CreateClauses []clause.Interface
QueryClauses []clause.Interface
UpdateClauses []clause.Interface
Expand All @@ -58,6 +58,7 @@ type Schema struct {
initialized chan struct{}
namer Namer
cacheStore *sync.Map
mux sync.RWMutex
}

func (schema Schema) String() string {
Expand All @@ -76,6 +77,8 @@ func (schema Schema) MakeSlice() reflect.Value {
}

func (schema Schema) LookUpField(name string) *Field {
schema.mux.RLock()
defer schema.mux.RUnlock()
if field, ok := schema.FieldsByDBName[name]; ok {
return field
}
Expand All @@ -99,9 +102,12 @@ func (schema Schema) LookUpFieldByBindName(bindNames []string, name string) *Fie
}
for i := len(bindNames) - 1; i >= 0; i-- {
find := strings.Join(bindNames[:i], ".") + "." + name
schema.mux.RLock()
if field, ok := schema.FieldsByBindName[find]; ok {
schema.mux.RUnlock()
return field
}
schema.mux.RUnlock()
}
return nil
}
Expand Down Expand Up @@ -185,7 +191,7 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam
FieldsByName: map[string]*Field{},
FieldsByBindName: map[string]*Field{},
FieldsByDBName: map[string]*Field{},
Relationships: Relationships{Relations: map[string]*Relationship{}},
Relationships: &Relationships{Relations: map[string]*Relationship{}},
cacheStore: cacheStore,
namer: namer,
initialized: make(chan struct{}),
Expand Down
2 changes: 1 addition & 1 deletion schema/schema_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func checkEmbeddedRelations(t *testing.T, actual map[string]*schema.Relationship
t.Errorf("failed to find relation by name %s", n)
} else {
checkSchemaRelation(t, &schema.Schema{
Relationships: schema.Relationships{
Relationships: &schema.Relationships{
Relations: map[string]*schema.Relationship{n: rel},
},
}, r)
Expand Down
Loading