diff --git a/association_test.go b/association_test.go index 2fbdb008..201eed53 100644 --- a/association_test.go +++ b/association_test.go @@ -50,8 +50,8 @@ func TestHasOneAndHasManyAssociation(t *testing.T) { t.Errorf("Comment 2 Should have post id") } - // comment3 := Comment{Content: "Comment 3", Post: Post{Title: "Title 3", Body: "Body 3"}} - // DB.Save(&comment3) + comment3 := Comment{Content: "Comment 3", Post: Post{Title: "Title 3", Body: "Body 3"}} + DB.Save(&comment3) } func TestRelated(t *testing.T) { diff --git a/model_struct.go b/model_struct.go index ecec9a32..828a4022 100644 --- a/model_struct.go +++ b/model_struct.go @@ -112,7 +112,7 @@ func (scope *Scope) generateSqlTag(field *StructField) { var pluralMapKeys = []*regexp.Regexp{regexp.MustCompile("ch$"), regexp.MustCompile("ss$"), regexp.MustCompile("sh$"), regexp.MustCompile("day$"), regexp.MustCompile("y$"), regexp.MustCompile("x$"), regexp.MustCompile("([^s])s?$")} var pluralMapValues = []string{"ches", "sses", "shes", "days", "ies", "xes", "${1}s"} -func (scope *Scope) GetModelStruct() *ModelStruct { +func (scope *Scope) GetModelStruct(noRelationship ...bool) *ModelStruct { var modelStruct ModelStruct reflectValue := reflect.Indirect(reflect.ValueOf(scope.Value)) @@ -210,9 +210,9 @@ func (scope *Scope) GetModelStruct() *ModelStruct { field.IsNormal = true } - if !field.IsNormal { + if !field.IsNormal && len(noRelationship) == 0 { gormSettings := parseTagSetting(field.Tag.Get("gorm")) - toModelStruct := scope.New(reflect.New(fieldStruct.Type).Interface()).GetModelStruct() + toModelStruct := scope.New(reflect.New(fieldStruct.Type).Interface()).GetModelStruct(true) getForeignField := func(column string, fields []*StructField) *StructField { for _, field := range fields { if field.Name == column || field.DBName == ToDBName(column) { @@ -327,7 +327,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct { modelStruct.StructFields = append(modelStruct.StructFields, field) } - if scope.db != nil { + if scope.db != nil && len(noRelationship) == 0 { scope.db.parent.ModelStructs[scopeType] = &modelStruct } diff --git a/structs_test.go b/structs_test.go index 24ddfd03..3bf76f3f 100644 --- a/structs_test.go +++ b/structs_test.go @@ -160,7 +160,7 @@ type Comment struct { Id int64 PostId int64 Content string - // Post Post + Post Post } // Scanner