From bc3728a18f380f28a007ba1100993e2c9f7e0288 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 10 Jul 2020 07:14:37 +0800 Subject: [PATCH] Fix concurrent map writes, close #3126 --- schema/schema.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/schema/schema.go b/schema/schema.go index b85bbd7e..66e02443 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -207,13 +207,13 @@ func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error) } } - cacheStore.Store(modelType, schema) - - // parse relations for unidentified fields - for _, field := range schema.Fields { - if field.DataType == "" && field.Creatable { - if schema.parseRelation(field); schema.err != nil { - return schema, schema.err + if _, loaded := cacheStore.LoadOrStore(modelType, schema); !loaded { + // parse relations for unidentified fields + for _, field := range schema.Fields { + if field.DataType == "" && field.Creatable { + if schema.parseRelation(field); schema.err != nil { + return schema, schema.err + } } } }