gorm
gorm copied to clipboard
Single Table Inheritance
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
Any updates on this question? Is there any unofficial guide I can follow for the implementation since official documentation does not include it?
Is there some doc about this?
I'm also interested in the answer, and/or a working example :)
Also interested in how to do this. We have several polymorphic types we would like to store using single table inheritance.
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
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.
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?
Is there any progress on this? Do you mind providing a working example @jinzhu ?