From 8a0c77e5fcbaf11a940e3f9eeb3535e99f53dfcf Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 3 Jan 2016 14:16:58 +0800 Subject: [PATCH] Fix all tests for GetModelStruct --- model_struct.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/model_struct.go b/model_struct.go index 13d2696b..b4ab67ca 100644 --- a/model_struct.go +++ b/model_struct.go @@ -254,6 +254,21 @@ func (scope *Scope) GetModelStruct() *ModelStruct { } else { relationship.Kind = "has_many" + if polymorphic := field.TagSettings["POLYMORPHIC"]; polymorphic != "" { + if polymorphicField := getForeignField(polymorphic+"ID", toScope.GetStructFields()); polymorphicField != nil { + if polymorphicType := getForeignField(polymorphic+"Type", toScope.GetStructFields()); polymorphicType != nil { + relationship.ForeignFieldNames = []string{polymorphicField.Name} + relationship.ForeignDBNames = []string{polymorphicField.DBName} + relationship.AssociationForeignFieldNames = []string{scope.PrimaryField().Name} + relationship.AssociationForeignDBNames = []string{scope.PrimaryField().DBName} + relationship.PolymorphicType = polymorphicType.Name + relationship.PolymorphicDBName = polymorphicType.DBName + polymorphicType.IsForeignKey = true + polymorphicField.IsForeignKey = true + } + } + } + // if no foreign keys if len(foreignKeys) == 0 { if len(associationForeignKeys) == 0 { @@ -311,6 +326,21 @@ func (scope *Scope) GetModelStruct() *ModelStruct { tagAssociationForeignKeys = strings.Split(field.TagSettings["ASSOCIATIONFOREIGNKEY"], ";") } + if polymorphic := field.TagSettings["POLYMORPHIC"]; polymorphic != "" { + if polymorphicField := getForeignField(polymorphic+"ID", toScope.GetStructFields()); polymorphicField != nil { + if polymorphicType := getForeignField(polymorphic+"Type", toScope.GetStructFields()); polymorphicType != nil { + relationship.ForeignFieldNames = []string{polymorphicField.Name} + relationship.ForeignDBNames = []string{polymorphicField.DBName} + relationship.AssociationForeignFieldNames = []string{scope.PrimaryField().Name} + relationship.AssociationForeignDBNames = []string{scope.PrimaryField().DBName} + relationship.PolymorphicType = polymorphicType.Name + relationship.PolymorphicDBName = polymorphicType.DBName + polymorphicType.IsForeignKey = true + polymorphicField.IsForeignKey = true + } + } + } + // Has One { var foreignKeys = tagForeignKeys