I have a series of categories of a type of Product
. There are only 3 of them so I can create a table for each. Lets call them SubProductA
, SubProductB
and SubProductC
. They are distinct enough that storing them in the parent Product
table would make a mess.
So naturally, I just allow the primary key to be the foreign key linking back to product for each of these. However, GORM seems to not be able to preload any field like this. Here is what I have (as an example) for one of these:
type SubProductA struct {
ProductID uint64 `gorm:"primary_key;ForeignKey:ProductID"`
Product Product
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
}
However, when I run a query like
db.Where("product_id=?", p.ID).Preload("Product").First(&subProduct).Error
The result is:
Can't preload field Product for models.SubProductA
.
I suspect this is because I'm not properly identifying the primary key as the foreign key as well. However, I have tried a bunch of different things and I get the same error.
Is there a way to make this work?