From 4b98b145b11d3da5a7562d89ca070134bd49b2b3 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 20 Mar 2015 11:49:16 +0800 Subject: [PATCH] Fix foreign db name in join table for multi primary keys relations --- join_table_handler.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/join_table_handler.go b/join_table_handler.go index 21e88fe1..b4299f5a 100644 --- a/join_table_handler.go +++ b/join_table_handler.go @@ -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, }) }