mirror of https://github.com/go-gorm/gorm.git
Create join table with computed foreign keys
This commit is contained in:
parent
6a7dda9a32
commit
25ba9487aa
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue