Fix foreign db name in join table for multi primary keys relations

This commit is contained in:
Jinzhu 2015-03-20 11:49:16 +08:00
parent 94a5ebe5b4
commit 4b98b145b1
1 changed files with 10 additions and 4 deletions

View File

@ -37,9 +37,12 @@ func (s *JoinTableHandler) Setup(relationship *Relationship, tableName string, s
s.Source = JoinTableSource{ModelType: source}
sourceScope := &Scope{Value: reflect.New(source).Interface()}
for _, primaryField := range sourceScope.GetModelStruct().PrimaryFields {
db := relationship.ForeignDBName
if relationship.ForeignDBName == "" {
relationship.ForeignFieldName = source.Name() + primaryField.Name
relationship.ForeignDBName = ToDBName(relationship.ForeignFieldName)
}
s.Source.ForeignKeys = append(s.Source.ForeignKeys, JoinTableForeignKey{
DBName: db,
DBName: relationship.ForeignDBName,
AssociationDBName: primaryField.DBName,
})
}
@ -47,9 +50,12 @@ func (s *JoinTableHandler) Setup(relationship *Relationship, tableName string, s
s.Destination = JoinTableSource{ModelType: destination}
destinationScope := &Scope{Value: reflect.New(destination).Interface()}
for _, primaryField := range destinationScope.GetModelStruct().PrimaryFields {
db := relationship.AssociationForeignDBName
if relationship.AssociationForeignDBName == "" {
relationship.AssociationForeignFieldName = destination.Name() + primaryField.Name
relationship.AssociationForeignDBName = ToDBName(relationship.AssociationForeignFieldName)
}
s.Destination.ForeignKeys = append(s.Destination.ForeignKeys, JoinTableForeignKey{
DBName: db,
DBName: relationship.AssociationForeignDBName,
AssociationDBName: primaryField.DBName,
})
}