I have a psql Database, and I am using the gorm library as well as the pq driver, as you see there is a many to many relationship for related products, however this throws out the error pq: column "product_id" specified more than once
is there a way to set an alias or am I going about this the wrong way?
type Product struct {
Id int64 `json:"_id"`
Price float32 `json:"price"`
Name string `sql:"size:255" json:"name"`
Description string `json:"description"`
Material string `json:"material"`
Color string `json:"color"`
ColorId int64 `json:"colorId"`
Categories []Category `gorm:"many2many:product_categories;" json:"categories"`
Images []Image `json:"images"`
Tags []Tag `gorm:"many2many:product_tags;" json:"tags"`
Main bool `json:"main"`
Available bool `json:"available"`
Slug string `json:"slug"`
CreatedAt time.Time `json:"createdAt"`
Related []Product `gorm:"many2many:related_products;" json:"related"`
}
I've found the solution of self-referencing many2many relationship. :D
type User struct {
Id int64
Related []Product `gorm:"foreignkey:product_id;associationforeignkey:related_product_id;many2many:related_products;" json:"related"`
}
Kinda old post but fortunately gorm have added lately this feature (reference: here).
In your case should be like this:
type Product struct {
Id int64 `json:"_id"`
.
.
.
Related []Product `gorm:"many2many:related_products;association_jointable_foreignkey:related_id"`
}