Polymorphism belongs to

Hello, I try to have a polymorphic relation like that:

TicketMessage struct {
  SenderType     string    `json:"senderType"`
  SenderID       uuid.UUID `json:"senderId"`
  AccountSender  *Account  `json:"sender,omitempty" gorm:"polymorphic:Sender;polymorphicValue:account"`
  CustomerSender *Customer `json:"sender,omitempty" gorm:"polymorphic:Sender;polymorphicValue:customer"`
}

But it's not working. I've checked in the documentation, nothing is said to do that. There is a way to do this? Thank you.

The document you expected this should be explained

https://gorm.io/docs/belongs_to.html

GORM Playground Link

https://github.com/go-gorm/playground/pull/234

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: jinzhu

this is not supported, you could only use polymorphism has one

Comment From: jinzhu

After few days thought, not going to support this, hard to support the preloading feature for this kind of relationship, maybe you should define it in polymorphism has one style

Comment From: meruiden

Im also looking for this functionality. instead of defining the polymorphic gorm attributes on the model without OwnerID and OwnerType I would like to define this only on the model containing the OwnerID and OwnerType and not on the model it refers to.

so instead of

type Dog struct {
    ID   int
    Name string
    Toy  Toy `gorm:"polymorphic:Owner;polymorphicValue:master"`
}

type Toy struct {
    ID        int
    Name      string
    OwnerID   int
    OwnerType string
}

im looking for something like this:


type Dog struct {
    ID   int
    Name string
}

type Toy struct {
    ID        int
    Name      string
    OwnerID   int  `gorm:"polymorphic:Owner;polymorphicID"`
    OwnerType string `gorm:"polymorphic:Owner;polymorphicType"`
}

Comment From: doganarif

@meruiden Did you find solution like this?