mirror of https://github.com/go-gorm/gorm.git
Fix multiple M2M relations on one table, close #3347
This commit is contained in:
parent
0273856e4d
commit
162367be7d
|
@ -254,12 +254,18 @@ func (schema *Schema) buildMany2ManyRelation(relation *Relationship, field *Fiel
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
joinTableFields = append(joinTableFields, reflect.StructField{
|
||||||
|
Name: schema.Name + field.Name,
|
||||||
|
Type: schema.ModelType,
|
||||||
|
Tag: `gorm:"-"`,
|
||||||
|
})
|
||||||
|
|
||||||
if relation.JoinTable, err = Parse(reflect.New(reflect.StructOf(joinTableFields)).Interface(), schema.cacheStore, schema.namer); err != nil {
|
if relation.JoinTable, err = Parse(reflect.New(reflect.StructOf(joinTableFields)).Interface(), schema.cacheStore, schema.namer); err != nil {
|
||||||
schema.err = err
|
schema.err = err
|
||||||
}
|
}
|
||||||
relation.JoinTable.Name = many2many
|
relation.JoinTable.Name = many2many
|
||||||
relation.JoinTable.Table = schema.namer.JoinTableName(many2many)
|
relation.JoinTable.Table = schema.namer.JoinTableName(many2many)
|
||||||
relation.JoinTable.PrimaryFields = make([]*Field, len(relation.JoinTable.Fields))
|
relation.JoinTable.PrimaryFields = make([]*Field, 0, len(relation.JoinTable.Fields))
|
||||||
|
|
||||||
relName := relation.Schema.Name
|
relName := relation.Schema.Name
|
||||||
relRefName := relation.FieldSchema.Name
|
relRefName := relation.FieldSchema.Name
|
||||||
|
@ -290,11 +296,12 @@ func (schema *Schema) buildMany2ManyRelation(relation *Relationship, field *Fiel
|
||||||
}
|
}
|
||||||
|
|
||||||
// build references
|
// build references
|
||||||
for idx, f := range relation.JoinTable.Fields {
|
for _, f := range relation.JoinTable.Fields {
|
||||||
|
if f.Creatable {
|
||||||
// use same data type for foreign keys
|
// use same data type for foreign keys
|
||||||
f.DataType = fieldsMap[f.Name].DataType
|
f.DataType = fieldsMap[f.Name].DataType
|
||||||
f.GORMDataType = fieldsMap[f.Name].GORMDataType
|
f.GORMDataType = fieldsMap[f.Name].GORMDataType
|
||||||
relation.JoinTable.PrimaryFields[idx] = f
|
relation.JoinTable.PrimaryFields = append(relation.JoinTable.PrimaryFields, f)
|
||||||
ownPriamryField := schema == fieldsMap[f.Name].Schema && ownFieldsMap[f.Name]
|
ownPriamryField := schema == fieldsMap[f.Name].Schema && ownFieldsMap[f.Name]
|
||||||
|
|
||||||
if ownPriamryField {
|
if ownPriamryField {
|
||||||
|
@ -322,6 +329,7 @@ func (schema *Schema) buildMany2ManyRelation(relation *Relationship, field *Fiel
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type guessLevel int
|
type guessLevel int
|
||||||
|
|
||||||
|
|
|
@ -267,3 +267,34 @@ func TestMany2ManyWithMultiPrimaryKeys(t *testing.T) {
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMultipleMany2Many(t *testing.T) {
|
||||||
|
type Thing struct {
|
||||||
|
ID int
|
||||||
|
}
|
||||||
|
|
||||||
|
type Person struct {
|
||||||
|
ID int
|
||||||
|
Likes []Thing `gorm:"many2many:likes"`
|
||||||
|
Dislikes []Thing `gorm:"many2many:dislikes"`
|
||||||
|
}
|
||||||
|
|
||||||
|
checkStructRelation(t, &Person{},
|
||||||
|
Relation{
|
||||||
|
Name: "Likes", Type: schema.Many2Many, Schema: "Person", FieldSchema: "Thing",
|
||||||
|
JoinTable: JoinTable{Name: "likes", Table: "likes"},
|
||||||
|
References: []Reference{
|
||||||
|
{"ID", "Person", "PersonID", "likes", "", true},
|
||||||
|
{"ID", "Thing", "ThingID", "likes", "", false},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Relation{
|
||||||
|
Name: "Dislikes", Type: schema.Many2Many, Schema: "Person", FieldSchema: "Thing",
|
||||||
|
JoinTable: JoinTable{Name: "dislikes", Table: "dislikes"},
|
||||||
|
References: []Reference{
|
||||||
|
{"ID", "Person", "PersonID", "dislikes", "", true},
|
||||||
|
{"ID", "Thing", "ThingID", "dislikes", "", false},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue