Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polymorphism Many To Many support #4004

Open
jinzhu opened this issue Jan 26, 2021 · 4 comments
Open

Polymorphism Many To Many support #4004

jinzhu opened this issue Jan 26, 2021 · 4 comments
Assignees

Comments

@jinzhu
Copy link
Member

jinzhu commented Jan 26, 2021

Describe the feature

Polymorphism Many To Many support

Motivation

Related Issues

@yaameen
Copy link

yaameen commented Sep 6, 2022

Any update on this? @jinzhu

@fgaluzzi
Copy link

fgaluzzi commented Sep 7, 2022

@yaameen I had the same problem mentioned here: #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;"`
}

@CmdrVasquess
Copy link

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?

@dkrieger
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants