Fix calculate fields for the first time

This commit is contained in:
Jinzhu 2015-09-11 07:35:57 +08:00
parent b46ca62c18
commit edc1f78530
2 changed files with 40 additions and 2 deletions

34
field_test.go Normal file
View File

@ -0,0 +1,34 @@
package gorm_test
import (
"testing"
"github.com/jinzhu/gorm"
)
type CalculateField struct {
gorm.Model
Name string
Children []CalculateFieldChild
Category CalculateFieldCategory
}
type CalculateFieldChild struct {
gorm.Model
CalculateFieldID uint
Name string
}
type CalculateFieldCategory struct {
gorm.Model
CalculateFieldID uint
Name string
}
func TestCalculateField(t *testing.T) {
var field CalculateField
fields := DB.NewScope(&field).Fields()
if fields["children"].Relationship == nil || fields["category"].Relationship == nil {
t.Errorf("Should calculate fields correctly for the first time")
}
}

View File

@ -153,7 +153,8 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
}
}
defer func() {
var finished = make(chan bool)
go func(finished chan bool) {
for _, field := range fields {
if !field.IsIgnored {
fieldStruct := field.Struct
@ -365,10 +366,13 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
}
modelStruct.StructFields = append(modelStruct.StructFields, field)
}
}()
finished <- true
}(finished)
modelStructs[scopeType] = &modelStruct
<-finished
return &modelStruct
}