@@ -75,9 +75,7 @@ func (schema *Schema) parseRelation(field *Field) *Relationship {
7575 }
7676 )
7777
78- cacheStore := schema .cacheStore
79-
80- if relation .FieldSchema , err = getOrParse (fieldValue , cacheStore , schema .namer ); err != nil {
78+ if relation .FieldSchema , err = getOrParse (fieldValue , schema .cacheStore , schema .namer ); err != nil {
8179 schema .err = fmt .Errorf ("failed to parse field: %s, error: %w" , field .Name , err )
8280 return nil
8381 }
@@ -147,6 +145,9 @@ func hasPolymorphicRelation(tagSettings map[string]string) bool {
147145}
148146
149147func (schema * Schema ) setRelation (relation * Relationship ) {
148+ schema .Relationships .Mux .Lock ()
149+ defer schema .Relationships .Mux .Unlock ()
150+
150151 // set non-embedded relation
151152 if rel := schema .Relationships .Relations [relation .Name ]; rel != nil {
152153 if len (rel .Field .BindNames ) > 1 {
@@ -590,13 +591,21 @@ func (schema *Schema) guessRelation(relation *Relationship, field *Field, cgl gu
590591 // build references
591592 for idx , foreignField := range foreignFields {
592593 // use same data type for foreign keys
594+ schema .Relationships .Mux .Lock ()
595+ if schema != foreignField .Schema {
596+ foreignField .Schema .Relationships .Mux .Lock ()
597+ }
593598 if copyableDataType (primaryFields [idx ].DataType ) {
594599 foreignField .DataType = primaryFields [idx ].DataType
595600 }
596601 foreignField .GORMDataType = primaryFields [idx ].GORMDataType
597602 if foreignField .Size == 0 {
598603 foreignField .Size = primaryFields [idx ].Size
599604 }
605+ schema .Relationships .Mux .Unlock ()
606+ if schema != foreignField .Schema {
607+ foreignField .Schema .Relationships .Mux .Unlock ()
608+ }
600609
601610 relation .References = append (relation .References , & Reference {
602611 PrimaryKey : primaryFields [idx ],
0 commit comments