forked from mirror/gorm
Fix calculate fields for the first time
This commit is contained in:
parent
b46ca62c18
commit
edc1f78530
|
@ -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")
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue