gorm多对多问题

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

gorm many to many proplem

问题

我有3个已经存在的表,现在我想建立它们之间的关系,但是我所做的一切都返回为空。问题可能是类别为空。接口表字段如下(app_products_playlist_category):

id
playlist_id
category_id

我认为问题可能出在外键上,但我不知道如何为现有的表设置它。

  1. type PlayListModel struct {
  2. gorm.Model
  3. Id int `gorm:"column:id" json:"id"`
  4. Published string `gorm:"column:published" json:"published"`
  5. Type_PlayLIST1 string `gorm:"column:type_playlist1" json:"type_playlist1"`
  6. Type_PlayLIST2 string `gorm:"column:type_playlist2" json:"type_playlist2"`
  7. Type_PlayLIST3 string `gorm:"column:type_playlist3" json:"type_playlist3"`
  8. Type_PlayLIST4 string `gorm:"column:type_playlist4" json:"type_playlist4"`
  9. Price int `gorm:"column:price" json:"price"`
  10. Price_Orginal int `gorm:"column:price_orginal" json:"price_orginal"`
  11. Main_img string `gorm:"column:main_img" json:"main_img"`
  12. UserId int `gorm:"column:user_id"`
  13. LIkesCount string `gorm:"column:likes_count" json:"likes_count"`
  14. ViewsCount string `gorm:"column:views_count" json:"views_count"`
  15. Content string `gorm:"column:content" json:"content"`
  16. Title string `gorm:"column:title" json:"title"`
  17. Categories []CategoryModel `gorm:"many2many:app_products_playlist_category;foreignKey:ID;references:ID"`
  18. }
  19. func (playlist *PlayListModel) TableName() string {
  20. return "app_products_playlist"
  21. }
  22. func GetPlayList(playlist *[]PlayListModel, order string) {
  23. Db.Order(order).Find(&playlist)
  24. }
  25. package models
  26. import "gorm.io/gorm"
  27. type CategoryModel struct {
  28. gorm.Model
  29. Id string `grom:column:"id" json:"id"`
  30. Name string `grom:column:"name" json:"name"`
  31. Img string `grom:column:"img" json:"img"`
  32. Slug string `grom:column:"slug" json:"slug"`
  33. Row string `grom:column:"row" json:"row"`
  34. ParenID string `grom:column:"parent_id" json:"parent_id"`
  35. PlayList []PlayListModel `gorm:"many2many:app_products_playlist_category"`
  36. }
  37. func (category *CategoryModel) TableName() string {
  38. return "app_category_category"
  39. }
  40. package models
  41. import "gorm.io/gorm"
  42. type PlayListCategoryModel struct {
  43. PlayListID int `gorm:"primary_key;column:playlist_id" json:"playlist_id"`
  44. CategoryID int `gorm:"primary_key;column:category_id" json:"category_id"`
  45. PlayListModel
  46. CategoryModel
  47. }
  48. func (PlayListCategoryModel) TableName() string {
  49. return "app_products_playlist_category"
  50. }
  51. func (PlayListCategoryModel) BeforeCreate(db *gorm.DB) error {
  52. err := db.SetupJoinTable(&PlayListModel{}, "Categories", &PlayListCategoryModel{})
  53. return err
  54. }
  1. -- 自动生成的定义
  2. create table app_products_playlist
  3. (
  4. id serial
  5. primary key,
  6. published varchar(10),
  7. title varchar(255),
  8. type_playlist1 varchar(255),
  9. type_playlist2 varchar(255),
  10. price integer,
  11. price_orginal integer,
  12. main_img varchar(100) not null,
  13. created_at timestamp with time zone not null,
  14. user_id integer
  15. constraint app_products_playlist_user_id_9fb269b5_fk_app_account_user_id
  16. references app_account_user
  17. deferrable initially deferred,
  18. likes_count integer,
  19. type_playlist3 varchar(50),
  20. views_count integer,
  21. content text,
  22. type_playlist4 varchar(50),
  23. deleted_at timestamp with time zone
  24. );
  25. alter table app_products_playlist
  26. owner to honardari;
  27. create index app_products_playlist_user_id_9fb269b5
  28. on app_products_playlist (user_id);
  29. -- 自动生成的定义
  30. create table app_category_category
  31. (
  32. id serial
  33. primary key,
  34. name varchar(255),
  35. important varchar(5),
  36. img varchar(100),
  37. pub_date date,
  38. slug varchar(50),
  39. row integer not null,
  40. lft integer not null
  41. constraint app_category_category_lft_check
  42. check (lft >= 0),
  43. rght integer not null
  44. constraint app_category_category_rght_check
  45. check (rght >= 0),
  46. tree_id integer not null
  47. constraint app_category_category_tree_id_check
  48. check (tree_id >= 0),
  49. level integer not null
  50. constraint app_category_category_level_check
  51. check (level >= 0),
  52. parent_id integer
  53. constraint app_category_categor_parent_id_0984d0d6_fk_app_categ
  54. references app_category_category
  55. deferrable initially deferred
  56. );
  57. alter table app_category_category
  58. owner to honardari;
  59. create index app_category_category_slug_6027a7e5
  60. on app_category_category (slug);
  61. create index app_category_category_slug_6027a7e5_like
  62. on app_category_category (slug varchar_pattern_ops);
  63. create index app_category_category_tree_id_35de0819
  64. on app_category_category (tree_id);
  65. create index app_category_category_parent_id_0984d0d6
  66. on app_category_category (parent_id);
  67. -- 自动生成的定义
  68. create table app_products_playlist_category
  69. (
  70. id serial
  71. primary key,
  72. playlist_id integer not null
  73. constraint app_products_playlis_playlist_id_f13be1da_fk_app_produ
  74. references app_products_playlist
  75. deferrable initially deferred,
  76. category_id integer not null
  77. constraint app_products_playlis_category_id_d6c7fd45_fk_app_categ
  78. references app_category_category
  79. deferrable initially deferred,
  80. constraint app_products_playlist_ca_playlist_id_category_id_88f46dfa_uniq
  81. unique (playlist_id, category_id)
  82. );
  83. alter table app_products_playlist_category
  84. owner to honardari;
  85. create index app_products_playlist_category_playlist_id_f13be1da
  86. on app_products_playlist_category (playlist_id);
  87. create index app_products_playlist_category_category_id_d6c7fd45
  88. on app_products_playlist_category (category_id);

[{ "ID": 0, "CreatedAt": "2021-12-28T19:15:38.761224+03:30", "UpdatedAt": "0001-01-01T00:00:00Z", "DeletedAt": null, "id": 9, "published": "2", "type_playlist1": "", "type_playlist2": "", "type_playlist3": "2", "type_playlist4": "2", "price": 0, "price_orginal": 1000, "main_img": "playlist/reza/download.jfif", "UserId": 2, "likes_count": "", "views_count": "", "content": "تست تست تست تست تستست تستست تستست", "title": "آموزش کیف چرم", "Categories": null }, { "ID": 0, "CreatedAt": "2022-01-03T15:13:11.485185+03:30", "UpdatedAt": "0001-01-01T00:00:00Z", "DeletedAt": null, "id": 10, "published": "2", "type_playlist1": "", "type_playlist2": "", "type_playlist3": "2", "type_playlist4": "2", "price": 0, "price_orginal": 2500, "main_img": "playlist/09301292813/QYfe8BCL.1.jpg", "UserId": 1, "likes_count": "", "views_count": "", "content": "سشدیندشسیسشsss ssdasd", "title": "آموزش کیف چرم 2", "Categories": null }]

英文:

I have 3 tables that already exist, now I want to relation , but everything I do comes back empty
proplem category is null
There are interface table fields (app_products_playlist_category):
id
playlist_id
category_id

I think the problem is with foregin key, but I do not know how to set it for existing tables.

  1. type PlayListModel struct {
  2. gorm.Model
  3. Id int `gorm:"column:id" json:"id"`
  4. Published string `gorm:"column:published" json:"published"`
  5. Type_PlayLIST1 string `gorm:"column:type_playlist1" json:"type_playlist1"`
  6. Type_PlayLIST2 string `gorm:"column:type_playlist2" json:"type_playlist2"`
  7. Type_PlayLIST3 string `gorm:"column:type_playlist3" json:"type_playlist3"`
  8. Type_PlayLIST4 string `gorm:"column:type_playlist4" json:"type_playlist4"`
  9. Price int `gorm:"column:price" json:"price"`
  10. Price_Orginal int `gorm:"column:price_orginal" json:"price_orginal"`
  11. Main_img string `gorm:"column:main_img" json:"main_img"`
  12. UserId int `gorm:"column:user_id"`
  13. LIkesCount string `gorm:"column:likes_count" json:"likes_count"`
  14. ViewsCount string `gorm:"column:views_count" json:"views_count"`
  15. Content string `gorm:"column:content" json:"content"`
  16. Title string `gorm:"column:title" json:"title"`
  17. Categories []CategoryModel `gorm:"many2many:app_products_playlist_category;foreignKey:ID;references:ID"`
  18. }
  19. func (playlist *PlayListModel) TableName() string {
  20. return "app_products_playlist"
  21. }
  22. func GetPlayList(playlist *[]PlayListModel, order string) {
  23. Db.Order(order).Find(&playlist)
  24. }
  25. package models
  26. import "gorm.io/gorm"
  27. type CategoryModel struct {
  28. gorm.Model
  29. Id string `grom:column:"id" json:"id"`
  30. Name string `grom:column:"name" json:"name"`
  31. Img string `grom:column:"img" json:"img"`
  32. Slug string `grom:column:"slug" json:"slug"`
  33. Row string `grom:column:"row" json:"row"`
  34. ParenID string `grom:column:"parent_id" json:"parent_id"`
  35. PlayList []PlayListModel `gorm:"many2many:app_products_playlist_category"`
  36. }
  37. func (category *CategoryModel) TableName() string {
  38. return "app_category_category"
  39. }
  40. package models
  41. import "gorm.io/gorm"
  42. type PlayListCategoryModel struct {
  43. PlayListID int `gorm:"primary_key;column:playlist_id" json:"playlist_id"`
  44. CategoryID int `gorm:"primary_key;column:category_id" json:"category_id"`
  45. PlayListModel
  46. CategoryModel
  47. }
  48. func (PlayListCategoryModel) TableName() string {
  49. return "app_products_playlist_category"
  50. }
  51. func (PlayListCategoryModel) BeforeCreate(db *gorm.DB) error {
  52. err := db.SetupJoinTable(&PlayListModel{}, "Categories", &PlayListCategoryModel{})
  53. return err
  54. }
  1. -- auto-generated definition
  2. create table app_products_playlist
  3. (
  4. id serial
  5. primary key,
  6. published varchar(10),
  7. title varchar(255),
  8. type_playlist1 varchar(255),
  9. type_playlist2 varchar(255),
  10. price integer,
  11. price_orginal integer,
  12. main_img varchar(100) not null,
  13. created_at timestamp with time zone not null,
  14. user_id integer
  15. constraint app_products_playlist_user_id_9fb269b5_fk_app_account_user_id
  16. references app_account_user
  17. deferrable initially deferred,
  18. likes_count integer,
  19. type_playlist3 varchar(50),
  20. views_count integer,
  21. content text,
  22. type_playlist4 varchar(50),
  23. deleted_at timestamp with time zone
  24. );
  25. alter table app_products_playlist
  26. owner to honardari;
  27. create index app_products_playlist_user_id_9fb269b5
  28. on app_products_playlist (user_id);
  29. -- auto-generated definition
  30. create table app_category_category
  31. (
  32. id serial
  33. primary key,
  34. name varchar(255),
  35. important varchar(5),
  36. img varchar(100),
  37. pub_date date,
  38. slug varchar(50),
  39. row integer not null,
  40. lft integer not null
  41. constraint app_category_category_lft_check
  42. check (lft >= 0),
  43. rght integer not null
  44. constraint app_category_category_rght_check
  45. check (rght >= 0),
  46. tree_id integer not null
  47. constraint app_category_category_tree_id_check
  48. check (tree_id >= 0),
  49. level integer not null
  50. constraint app_category_category_level_check
  51. check (level >= 0),
  52. parent_id integer
  53. constraint app_category_categor_parent_id_0984d0d6_fk_app_categ
  54. references app_category_category
  55. deferrable initially deferred
  56. );
  57. alter table app_category_category
  58. owner to honardari;
  59. create index app_category_category_slug_6027a7e5
  60. on app_category_category (slug);
  61. create index app_category_category_slug_6027a7e5_like
  62. on app_category_category (slug varchar_pattern_ops);
  63. create index app_category_category_tree_id_35de0819
  64. on app_category_category (tree_id);
  65. create index app_category_category_parent_id_0984d0d6
  66. on app_category_category (parent_id);
  67. -- auto-generated definition
  68. create table app_products_playlist_category
  69. (
  70. id serial
  71. primary key,
  72. playlist_id integer not null
  73. constraint app_products_playlis_playlist_id_f13be1da_fk_app_produ
  74. references app_products_playlist
  75. deferrable initially deferred,
  76. category_id integer not null
  77. constraint app_products_playlis_category_id_d6c7fd45_fk_app_categ
  78. references app_category_category
  79. deferrable initially deferred,
  80. constraint app_products_playlist_ca_playlist_id_category_id_88f46dfa_uniq
  81. unique (playlist_id, category_id)
  82. );
  83. alter table app_products_playlist_category
  84. owner to honardari;
  85. create index app_products_playlist_category_playlist_id_f13be1da
  86. on app_products_playlist_category (playlist_id);
  87. create index app_products_playlist_category_category_id_d6c7fd45
  88. on app_products_playlist_category (category_id);

>[{"ID":0,"CreatedAt":"2021-12-28T19:15:38.761224+03:30","UpdatedAt":"0001-01-01T00:00:00Z","DeletedAt":null,"id":9,"published":"2","type_playlist1":"","type_playlist2":"","type_playlist3":"2","type_playlist4":"2","price":0,"price_orginal":1000,"main_img":"playlist/reza/download.jfif","UserId":2,"likes_count":"","views_count":"","content":"تست تست تست تست تستست تستست تستست","title":"آموزش کیف چرم","Categories":null},{"ID":0,"CreatedAt":"2022-01-03T15:13:11.485185+03:30","UpdatedAt":"0001-01-01T00:00:00Z","DeletedAt":null,"id":10,"published":"2","type_playlist1":"","type_playlist2":"","type_playlist3":"2","type_playlist4":"2","price":0,"price_orginal":2500,"main_img":"playlist/09301292813/QYfe8BCL.1.jpg","UserId":1,"likes_count":"","views_count":"","content":"سشدیندشسیسشsss ssdasd","title":"آموزش کیف چرم 2","Categories":null}]

Heading

gorm多对多问题
gorm多对多问题
gorm多对多问题

答案1

得分: 1

假设你已经创建了一个带有categoriesplaylistModel,就像这样:

  1. db.Create(&PlayListModel{
  2. Published: time.Now().String(),
  3. Categories: []CategoryModel{
  4. {Name: "test-category"},
  5. },
  6. })

要执行一个查询,返回包含playlistModel和其中的categories的结果,你需要使用Preload方法,像这样:

  1. var playlist []*PlayListModel
  2. db.Preload("Categories").Find(&playlist)
英文:

Supposing that you've created a playlistModel with categories, like this:

  1. db.Create(&PlayListModel{
  2. Published: time.Now().String(),
  3. Categories: []CategoryModel{
  4. {Name: "test-category"},
  5. },
  6. })

For do a query that returns playlistModel and categories inside, you need use Preload method, in this way:

  1. var playlist []*PlayListModel
  2. db.Preload("Categories").Find(&playlist)

答案2

得分: 0

将以下内容翻译为中文:

将以下内容添加到Categories []CategoryModel中:

  1. ;joinForeignKey:playlist_id;joinReferences:category_id

如下所示:

  1. type PlayListModel struct {
  2. gorm.Model
  3. Id int `gorm:"column:id" json:"id"`
  4. Published string `gorm:"column:published" json:"published"`
  5. Type_PlayLIST1 string `gorm:"column:type_playlist1" json:"type_playlist1"`
  6. Type_PlayLIST2 string `gorm:"column:type_playlist2" json:"type_playlist2"`
  7. Type_PlayLIST3 string `gorm:"column:type_playlist3" json:"type_playlist3"`
  8. Type_PlayLIST4 string `gorm:"column:type_playlist4" json:"type_playlist4"`
  9. Price int `gorm:"column:price" json:"price"`
  10. Price_Orginal int `gorm:"column:price_orginal" json:"price_orginal"`
  11. Main_img string `gorm:"column:main_img" json:"main_img"`
  12. UserId int `gorm:"column:user_id"`
  13. LIkesCount string `gorm:"column:likes_count" json:"likes_count"`
  14. ViewsCount string `gorm:"column:views_count" json:"views_count"`
  15. Content string `gorm:"column:content" json:"content"`
  16. Title string `gorm:"column:title" json:"title"`
  17. Categories []CategoryModel `gorm:"many2many:app_products_playlist_category;joinForeignKey:playlist_id;joinReferences:category_id"`
  18. }
英文:

Add:

  1. ;joinForeignKey:playlist_id;joinReferences:category_id

to

  1. Categories []CategoryModel

As done in the following:

  1. type PlayListModel struct {
  2. gorm.Model
  3. Id int `gorm:"column:id" json:"id"`
  4. Published string `gorm:"column:published" json:"published"`
  5. Type_PlayLIST1 string `gorm:"column:type_playlist1" json:"type_playlist1"`
  6. Type_PlayLIST2 string `gorm:"column:type_playlist2" json:"type_playlist2"`
  7. Type_PlayLIST3 string `gorm:"column:type_playlist3" json:"type_playlist3"`
  8. Type_PlayLIST4 string `gorm:"column:type_playlist4" json:"type_playlist4"`
  9. Price int `gorm:"column:price" json:"price"`
  10. Price_Orginal int `gorm:"column:price_orginal" json:"price_orginal"`
  11. Main_img string `gorm:"column:main_img" json:"main_img"`
  12. UserId int `gorm:"column:user_id"`
  13. LIkesCount string `gorm:"column:likes_count" json:"likes_count"`
  14. ViewsCount string `gorm:"column:views_count" json:"views_count"`
  15. Content string `gorm:"column:content" json:"content"`
  16. Title string `gorm:"column:title" json:"title"`
  17. Categories []CategoryModel `gorm:"many2many:app_products_playlist_category;joinForeignKey:playlist_id;joinReferences:category_id"`
  18. }

huangapple
  • 本文由 发表于 2022年1月4日 21:52:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/70579831.html
匿名

发表评论

匿名网友

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

确定