From 9ca84b3dde0400ac69cc6cbf8dd8ec531f9638bd Mon Sep 17 00:00:00 2001 From: Vladimir Avtsenov Date: Sun, 12 Jan 2025 14:49:06 +0300 Subject: [PATCH] fix concurrent map writes (#7298) --- schema/relationship.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/schema/relationship.go b/schema/relationship.go index 32676b399..def4a5958 100644 --- a/schema/relationship.go +++ b/schema/relationship.go @@ -5,6 +5,7 @@ import ( "fmt" "reflect" "strings" + "sync" "github.com/jinzhu/inflection" "golang.org/x/text/cases" @@ -32,6 +33,8 @@ type Relationships struct { Relations map[string]*Relationship EmbeddedRelations map[string]*Relationships + + Mux sync.RWMutex } type Relationship struct { @@ -98,9 +101,10 @@ func (schema *Schema) parseRelation(field *Field) *Relationship { } if relation.Type == has { - // don't add relations to embedded schema, which might be shared if relation.FieldSchema != relation.Schema && relation.Polymorphic == nil && field.OwnerSchema == nil { + relation.FieldSchema.Relationships.Mux.Lock() relation.FieldSchema.Relationships.Relations["_"+relation.Schema.Name+"_"+relation.Name] = relation + relation.FieldSchema.Relationships.Mux.Unlock() } switch field.IndirectFieldType.Kind() {