go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
36.14k stars 3.88k forks source link

Single Table Inheritance #4981

Open rohitjha941 opened 2 years ago

rohitjha941 commented 2 years ago

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

ChaminW commented 1 year ago

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

leandrorebelo commented 1 year ago

Is there some doc about this?

ldaverio commented 1 year ago

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

abferm commented 1 year ago

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

JokerCatz commented 1 year ago

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

atheken commented 6 months ago

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.

yoavke commented 6 months ago

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?

rafikiassumani commented 3 months ago

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