Passed all tests for multiple primary keys

This commit is contained in:
Jinzhu 2015-07-30 22:59:25 +08:00
parent f50956cfef
commit f00b95d305
2 changed files with 15 additions and 13 deletions

View File

@ -174,7 +174,7 @@ func (association *Association) Count() int {
if relationship.PolymorphicType != "" { if relationship.PolymorphicType != "" {
query = query.Where(fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(relationship.PolymorphicDBName)), scope.TableName()) query = query.Where(fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(relationship.PolymorphicDBName)), scope.TableName())
} }
query.Count(&count) query.Table(newScope.TableName()).Count(&count)
} else if relationship.Kind == "belongs_to" { } else if relationship.Kind == "belongs_to" {
query := scope.DB() query := scope.DB()
for idx, foreignKey := range relationship.ForeignDBNames { for idx, foreignKey := range relationship.ForeignDBNames {

View File

@ -195,6 +195,8 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
if polymorphicType := getForeignField(polymorphic+"Type", toScope.GetStructFields()); polymorphicType != nil { if polymorphicType := getForeignField(polymorphic+"Type", toScope.GetStructFields()); polymorphicType != nil {
relationship.ForeignFieldNames = []string{polymorphicField.Name} relationship.ForeignFieldNames = []string{polymorphicField.Name}
relationship.ForeignDBNames = []string{polymorphicField.DBName} relationship.ForeignDBNames = []string{polymorphicField.DBName}
relationship.AssociationForeignFieldNames = []string{scope.PrimaryField().Name}
relationship.AssociationForeignDBNames = []string{scope.PrimaryField().DBName}
relationship.PolymorphicType = polymorphicType.Name relationship.PolymorphicType = polymorphicType.Name
relationship.PolymorphicDBName = polymorphicType.DBName relationship.PolymorphicDBName = polymorphicType.DBName
polymorphicType.IsForeignKey = true polymorphicType.IsForeignKey = true
@ -298,32 +300,6 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
} }
} }
continue continue
} else {
if len(foreignKeys) == 0 {
for _, f := range toScope.PrimaryFields() {
if foreignField := getForeignField(field.Name+f.Name, fields); foreignField != nil {
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, f.Name)
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, f.DBName)
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
foreignField.IsForeignKey = true
}
}
} else {
for _, foreignKey := range foreignKeys {
if foreignField := getForeignField(foreignKey, fields); foreignField != nil {
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, toScope.PrimaryField().Name)
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, toScope.PrimaryField().DBName)
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
foreignField.IsForeignKey = true
}
}
}
if len(relationship.ForeignFieldNames) != 0 {
relationship.Kind = "belongs_to"
field.Relationship = relationship
} else { } else {
if len(foreignKeys) == 0 { if len(foreignKeys) == 0 {
for _, f := range scope.PrimaryFields() { for _, f := range scope.PrimaryFields() {
@ -350,6 +326,32 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
if len(relationship.ForeignFieldNames) != 0 { if len(relationship.ForeignFieldNames) != 0 {
relationship.Kind = "has_one" relationship.Kind = "has_one"
field.Relationship = relationship field.Relationship = relationship
} else {
if len(foreignKeys) == 0 {
for _, f := range toScope.PrimaryFields() {
if foreignField := getForeignField(field.Name+f.Name, fields); foreignField != nil {
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, f.Name)
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, f.DBName)
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
foreignField.IsForeignKey = true
}
}
} else {
for _, foreignKey := range foreignKeys {
if foreignField := getForeignField(foreignKey, fields); foreignField != nil {
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, toScope.PrimaryField().Name)
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, toScope.PrimaryField().DBName)
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
foreignField.IsForeignKey = true
}
}
}
if len(relationship.ForeignFieldNames) != 0 {
relationship.Kind = "belongs_to"
field.Relationship = relationship
} }
} }
} }