forked from mirror/gorm
Fix create join table with multi primary keys
This commit is contained in:
parent
e2eef50fb4
commit
67266ebdb3
|
@ -445,13 +445,19 @@ func (scope *Scope) createJoinTable(field *StructField) {
|
|||
joinTableHandler := relationship.JoinTableHandler
|
||||
joinTable := joinTableHandler.Table(scope.db)
|
||||
if !scope.Dialect().HasTable(scope, joinTable) {
|
||||
primaryKeySqlType := scope.Dialect().SqlTag(scope.PrimaryField().Field, 255, false)
|
||||
scope.Err(scope.NewDB().Exec(fmt.Sprintf("CREATE TABLE %v (%v)",
|
||||
scope.Quote(joinTable),
|
||||
strings.Join([]string{
|
||||
scope.Quote(relationship.ForeignDBName) + " " + primaryKeySqlType,
|
||||
scope.Quote(relationship.AssociationForeignDBName) + " " + primaryKeySqlType}, ",")),
|
||||
).Error)
|
||||
toScope := &Scope{Value: reflect.New(field.Struct.Type).Interface()}
|
||||
|
||||
var sqlTypes []string
|
||||
for _, s := range []*Scope{scope, toScope} {
|
||||
for _, primaryField := range s.GetModelStruct().PrimaryFields {
|
||||
value := reflect.Indirect(reflect.New(primaryField.Struct.Type))
|
||||
primaryKeySqlType := scope.Dialect().SqlTag(value, 255, false)
|
||||
dbName := ToDBName(s.GetModelStruct().ModelType.Name() + primaryField.Name)
|
||||
sqlTypes = append(sqlTypes, scope.Quote(dbName)+" "+primaryKeySqlType)
|
||||
}
|
||||
}
|
||||
|
||||
scope.Err(scope.NewDB().Exec(fmt.Sprintf("CREATE TABLE %v (%v)", scope.Quote(joinTable), strings.Join(sqlTypes, ","))).Error)
|
||||
}
|
||||
scope.NewDB().Table(joinTable).AutoMigrate(joinTableHandler)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue