gorm icon indicating copy to clipboard operation
gorm copied to clipboard

Single Table Inheritance

Open rohitjha941 opened this issue 3 years ago • 9 comments

Your Question

Gorm homepage explains that it has support for single-table inheritance. But it has not been mentioned how to implement it in Association section of the doc

The document you expected this should be explained

I expect it to be mentioned in Association section of the doc

Expected answer

rohitjha941 avatar Jan 05 '22 10:01 rohitjha941

Any updates on this question? Is there any unofficial guide I can follow for the implementation since official documentation does not include it?

ChaminW avatar Sep 01 '22 06:09 ChaminW

Is there some doc about this?

leandrorebelo avatar Sep 13 '22 14:09 leandrorebelo

I'm also interested in the answer, and/or a working example :)

ldaverio avatar Sep 24 '22 14:09 ldaverio

Also interested in how to do this. We have several polymorphic types we would like to store using single table inheritance.

abferm avatar Nov 29 '22 17:11 abferm

I know how to write Ruby / Rails / Golang , and I think this is easy way to like

type Animal struct {
  gorm.Model
  Type      string // Rails.ActiveRecord reserved word , it mean real model name , like "Animal" / "Cat" / "Dog"

  // same as belongs_to   (self has target ID)
  ParentID  int64
  Parent    Animal `gorm:"foreignKey:ParentID"`

  // same as has_many     (target table has my ID)
  Childs  []Animal `gorm:"foreignKey:ParentID"`
}
type Cat struct {
  // same as Animal struct , dont change Animal to Cat
}
type Dog struct {
  // same as Animal struct , dont change Animal to Dog
}

// every model use same table name
func (Animal) TableName() string {return "dittos"}
func (Cat   ) TableName() string {return "dittos"}
func (Dog   ) TableName() string {return "dittos"}

and Animal need write some code to redirect to (or deep copy) current model / struct , like

animal.Is("Dog") => return bool
animal.GetReal() => return "Dog" , interface{}(Dog{})

and sorry , code is not tested , just show how to made it

JokerCatz avatar Jan 31 '23 12:01 JokerCatz

Would it be possible to get a reference in the gorm code for how STI is supported? If I could locate how/when type discriminators were defined, I could probably work through and provide an example. Thanks for all the work you've put into this module over the years.

atheken avatar Jan 10 '24 22:01 atheken

I have recently begun reading the documentation, and while going through the overview page, I noticed a reference to 'Single-table inheritance' that piqued my curiosity. I need it for a project I am currently working on. I searched for a page that explains how it works in GORM, but I couldn't find any relevant information. As a result, I decided to Google it and came across this thread.

Could it be possible that GORM does not currently support Single-table inheritance?

yoavke avatar Jan 13 '24 11:01 yoavke

Is there any progress on this? Do you mind providing a working example @jinzhu ?

rafikiassumani avatar Apr 04 '24 20:04 rafikiassumani