Skip to content

Commit 426eae3

Browse files
committed
fix concurrent map writes
1 parent 6bfccf8 commit 426eae3

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

schema/relationship.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"reflect"
77
"strings"
8+
"sync"
89

910
"github.com/jinzhu/inflection"
1011
"golang.org/x/text/cases"
@@ -32,6 +33,8 @@ type Relationships struct {
3233
Relations map[string]*Relationship
3334

3435
EmbeddedRelations map[string]*Relationships
36+
37+
Mux *sync.RWMutex
3538
}
3639

3740
type Relationship struct {
@@ -98,9 +101,10 @@ func (schema *Schema) parseRelation(field *Field) *Relationship {
98101
}
99102

100103
if relation.Type == has {
101-
// don't add relations to embedded schema, which might be shared
102104
if relation.FieldSchema != relation.Schema && relation.Polymorphic == nil && field.OwnerSchema == nil {
105+
relation.FieldSchema.Relationships.Mux.Lock()
103106
relation.FieldSchema.Relationships.Relations["_"+relation.Schema.Name+"_"+relation.Name] = relation
107+
relation.FieldSchema.Relationships.Mux.Unlock()
104108
}
105109

106110
switch field.IndirectFieldType.Kind() {

schema/schema.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam
184184
FieldsByName: map[string]*Field{},
185185
FieldsByBindName: map[string]*Field{},
186186
FieldsByDBName: map[string]*Field{},
187-
Relationships: Relationships{Relations: map[string]*Relationship{}},
187+
Relationships: Relationships{Relations: map[string]*Relationship{}, Mux: &sync.RWMutex{}},
188188
cacheStore: cacheStore,
189189
namer: namer,
190190
initialized: make(chan struct{}),

0 commit comments

Comments
 (0)