Create join table with computed foreign keys

This commit is contained in:
Jinzhu 2015-07-31 16:33:44 +08:00
parent 6a7dda9a32
commit 25ba9487aa
1 changed files with 12 additions and 5 deletions

View File

@ -459,12 +459,19 @@ func (scope *Scope) createJoinTable(field *StructField) {
toScope := &Scope{Value: reflect.New(field.Struct.Type).Interface()} toScope := &Scope{Value: reflect.New(field.Struct.Type).Interface()}
var sqlTypes []string var sqlTypes []string
for _, s := range []*Scope{scope, toScope} { for idx, fieldName := range relationship.ForeignFieldNames {
for _, primaryField := range s.GetModelStruct().PrimaryFields { if field, ok := scope.Fields()[fieldName]; ok {
value := reflect.Indirect(reflect.New(primaryField.Struct.Type)) value := reflect.Indirect(reflect.New(field.Struct.Type))
primaryKeySqlType := scope.Dialect().SqlTag(value, 255, false) primaryKeySqlType := scope.Dialect().SqlTag(value, 255, false)
dbName := ToDBName(s.GetModelStruct().ModelType.Name() + primaryField.Name) sqlTypes = append(sqlTypes, scope.Quote(relationship.ForeignDBNames[idx])+" "+primaryKeySqlType)
sqlTypes = append(sqlTypes, scope.Quote(dbName)+" "+primaryKeySqlType) }
}
for idx, fieldName := range relationship.AssociationForeignFieldNames {
if field, ok := toScope.Fields()[fieldName]; ok {
value := reflect.Indirect(reflect.New(field.Struct.Type))
primaryKeySqlType := scope.Dialect().SqlTag(value, 255, false)
sqlTypes = append(sqlTypes, scope.Quote(relationship.AssociationForeignDBNames[idx])+" "+primaryKeySqlType)
} }
} }