How can I go about making a many2many relationship to self in golang gorm?

huangapple go评论93阅读模式
英文:

How can I go about making a many2many relationship to self in golang gorm?

问题

我有一个psql数据库,并且我正在使用gorm库和pq驱动程序。正如你所看到的,这里有一个多对多的关系来关联产品,但是这会抛出错误"pq: column "product_id" specified more than once"。有没有办法设置别名,或者我这样做的方式是错误的?

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 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"`
}

答案1

得分: 2

我找到了自引用多对多关系的解决方案。 How can I go about making a many2many relationship to self in golang gorm?

type User struct {
    Id int64
    Related     []Product  `gorm:"foreignkey:product_id;associationforeignkey:related_product_id;many2many:related_products;" json:"related"`        
}
英文:

I've found the solution of self-referencing many2many relationship. How can I go about making a many2many relationship to self in golang gorm?

type User struct {
    Id int64
    Related     []Product  `gorm:"foreignkey:product_id;associationforeignkey:related_product_id;many2many:related_products;" json:"related"`        
}

答案2

得分: 1

以下是翻译好的内容:

有点旧的帖子,但幸运的是,gorm最近添加了这个功能(参考:这里)。

在你的情况下应该是这样的:

type Product struct {
  Id          int64      `json:"_id"`
  .
  .
  .
  Related     []Product `gorm:"many2many:related_products;association_jointable_foreignkey:related_id"`
}
英文:

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"`
}

huangapple
  • 本文由 发表于 2014年12月28日 08:57:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/27672876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定