Fix all tests for GetModelStruct

This commit is contained in:
Jinzhu 2016-01-03 14:16:58 +08:00
parent 4bc06a21c1
commit 8a0c77e5fc
1 changed files with 30 additions and 0 deletions

View File

@ -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