Describe the feature
Polymorphism Many To Many support
Motivation
Related Issues
Comment From: yaameen
Any update on this? @jinzhu
Comment From: fgaluzzi
@yaameen I had the same problem mentioned here: https://github.com/go-gorm/gorm/issues/4270 and i solved it with 2 different join tables, i don't know if it solves your problem too, can you share any example?
type Label struct {
LabeID uint `gorm:"primarykey"`
Name string
}
type Fruit struct {
gorm.Model
Labels []*Label `gorm:"many2many:fruit_labels;"`
}
type Vegetable struct {
gorm.Model
Labels []*Label `gorm:"many2many:vegetable_labels;"`
}
Comment From: CmdrVasquess
Actually that's what I miss most with GORM: Real OO-like ploymorphism support
From long years of OOA/-D/-P I consider this situation the most common use case for polymorphism (UML style):
OwningClass [1] <>--- [0..*] AbstractBaseClass
with AbstractBaseClass having several specializations. I.e. for me its not so much about many2many but about having the polymorphism at the n-end of the 1:n relationship. And that's also what I am used to from all those ORM's for C++, Java.
As far as I understand GORM the polymorphic type can only be at the 1-end of the relation, right?
Comment From: dkrieger
if you make an explicit model for your many-to-many relation for A-B (B being the desired polymorphic model), let's call it C, you then have A-C one-to-many and C-B many-to-one and you can use polymorphism. I think it's reasonable to add "polymorphism" to the list of customizations of a join table that warrant making the join table explicit, so this seems like it should be converted to a documentation request rather than a feature request.
Comment From: simontol
@dkrieger would you please provide a working example? Thanks
Comment From: sushilprj
@simontol - Did you get the response? I have a similar requirement.