我需要将这个SQL查询用于GORM查询

jdg4fx2g  于 2023-09-28  发布在  Go
关注(0)|答案(1)|浏览(129)

我有这个SQL查询。

  1. select c.id,
  2. c.title as collection,
  3. (select json_agg(g2.*)
  4. from (select g.id,
  5. g.title,
  6. g.price,
  7. g.description,
  8. (select json_agg(items.*)
  9. from (select id, size, color, quantity, images
  10. from items t
  11. where t.id = any (g.items_id)) items) as items
  12. from "groups" g
  13. where g.id = any (c.group_id)) g2) "group"
  14. from collections c where c.publication_date <= now();

这是我在数据库中的模型。

  1. create table collections (
  2. id serial primary key NOT null,
  3. title varchar (20) unique not null,
  4. group_id int[],
  5. publication_date timestamp not null,
  6. created_at timestamp NOT null default now(),
  7. updated_at timestamp NOT null default now(),
  8. created_by int NOT NULL,
  9. updated_by int NOT NULL
  10. );
  11. insert into collections(title, group_id, publication_date, created_by, updated_by)
  12. values
  13. ('verano', '{1,2}', now(), 1, 1),
  14. ('premium', '{2}', now(), 1, 1),
  15. ('moda 2023','{3}', now(), 1, 1),
  16. ('moda 2024','{4}', now(), 1, 1);
  17. create table "groups" (
  18. id serial primary key NOT null,
  19. title varchar (50) unique not null,
  20. items_id int[],
  21. price float8 not null,
  22. description varchar (250),
  23. created_at timestamp NOT null default now(),
  24. updated_at timestamp NOT null default now(),
  25. created_by int NOT NULL,
  26. updated_by int NOT NULL
  27. );
  28. insert into "groups"(title, items_id, price, description ,created_by, updated_by)
  29. values
  30. ('Blusa De Dama Mikey Mouse', '{1,2}', 5.99,'Blusa de dama con micro durazno, distintos colores.' , 1, 1),
  31. ('Sueter Tricolor', '{3,4}', 13.85,'Blusa de dama con micro durazno, distintos colores.' , 1, 1),
  32. ('Mono Adidas', '{5,6}', 17.99,'Blusa de dama con micro durazno, distintos colores.' , 1, 1),
  33. ('Franela Apolo', '{}', 8.99,'Franela Apolo, distintos colores.' , 1, 1);
  34. create table items (
  35. id serial primary key NOT null,
  36. "size" char not null,
  37. color varchar (10) not null,
  38. quantity int not null,
  39. images text[] not null,
  40. created_at timestamp NOT null default now(),
  41. updated_at timestamp NOT null default now(),
  42. created_by int NOT NULL,
  43. updated_by int NOT NULL
  44. );
  45. insert into items (size, color, quantity, images, created_by, updated_by)
  46. values
  47. ('U', 'Negro', 150, '{none}', 1, 1),
  48. ('S', 'Blanco', 150, '{none}', 1, 1),
  49. ('M', 'Gris', 150, '{none}', 1, 1),
  50. ('U', 'Gris', 220, '{none}', 1, 1),
  51. ('S', 'Verde', 220, '{none}', 1, 1),
  52. ('M', 'Negro', 220, '{none}', 1, 1);

当我在DBeaver中使用该模型时,上面的查询可以很好地工作,但我不知道如何在Go with GORM中使用它。
我尝试了这种方法,但组数据返回为空。

  1. err := p.DB.WithContext(ctx).Raw("select c.id, c.title, (select json_agg(g2.*) from (select g.id, g.title, g.price, g.description (select json_agg(items.*) from (select id, size, color, quantity, images from items t where t.id = any (g.items_id)) items) as items from groups g where g.id = any (c.group_id)) g2) as group from collections c where c.publication_date <= now()").
  2. Scan(&products).
  3. Error

这就是我得到的结果。

  1. [
  2. {
  3. "ID": 1,
  4. "Title": "verano",
  5. "PublicationDate": "0001-01-01T00:00:00Z",
  6. "Group": null,
  7. "CreatedAt": "0001-01-01T00:00:00Z",
  8. "UpdatedAt": "0001-01-01T00:00:00Z",
  9. "CreatedBy": 0,
  10. "UpdatedBy": 0
  11. },
  12. {
  13. "ID": 2,
  14. "Title": "premium",
  15. "PublicationDate": "0001-01-01T00:00:00Z",
  16. "Group": null,
  17. "CreatedAt": "0001-01-01T00:00:00Z",
  18. "UpdatedAt": "0001-01-01T00:00:00Z",
  19. "CreatedBy": 0,
  20. "UpdatedBy": 0
  21. },
  22. {
  23. "ID": 3,
  24. "Title": "moda 2023",
  25. "PublicationDate": "0001-01-01T00:00:00Z",
  26. "Group": null,
  27. "CreatedAt": "0001-01-01T00:00:00Z",
  28. "UpdatedAt": "0001-01-01T00:00:00Z",
  29. "CreatedBy": 0,
  30. "UpdatedBy": 0
  31. },
  32. {
  33. "ID": 4,
  34. "Title": "moda 2024",
  35. "PublicationDate": "0001-01-01T00:00:00Z",
  36. "Group": null,
  37. "CreatedAt": "0001-01-01T00:00:00Z",
  38. "UpdatedAt": "0001-01-01T00:00:00Z",
  39. "CreatedBy": 0,
  40. "UpdatedBy": 0
  41. }
  42. ]

这是我在后端使用的模型。

  1. type Collection struct {
  2. ID int64
  3. Title string
  4. PublicationDate time.Time
  5. Group []Group `gorm:"embedded"`
  6. CreatedAt time.Time
  7. UpdatedAt time.Time
  8. CreatedBy uint
  9. UpdatedBy uint
  10. }
  11. type Group struct {
  12. ID int64
  13. TitleG string
  14. Price float64
  15. Description string
  16. Items []Item `gorm:"embedded"`
  17. CreatedAt time.Time
  18. UpdatedAt time.Time
  19. CreatedBy uint
  20. UpdatedBy uint
  21. }
  22. type Item struct {
  23. ID int64
  24. Size string
  25. Color string
  26. Quantity int
  27. Images []string
  28. CreatedAt time.Time
  29. UpdatedAt time.Time
  30. CreatedBy uint
  31. UpdatedBy uint
  32. }

我期待着你的评论。谢谢你,谢谢!

q5lcpyga

q5lcpyga1#

如果想用gorm获取嵌套的JSON,就必须定义模型之间的关系,然后使用Preload函数加载相关数据。试着用这种方式修改你的后端模型;

  1. type Collection struct {
  2. ID int64
  3. Title string
  4. PublicationDate time.Time
  5. Groups []Group `gorm:"foreignKey:CollectionID"`
  6. CreatedAt time.Time
  7. UpdatedAt time.Time
  8. CreatedBy uint
  9. UpdatedBy uint
  10. }
  11. type Group struct {
  12. ID int64
  13. Title string
  14. Price float64
  15. Description string
  16. Items []Item `gorm:"foreignKey:GroupID"`
  17. CreatedAt time.Time
  18. UpdatedAt time.Time
  19. CreatedBy uint
  20. UpdatedBy uint
  21. }
  22. type Item struct {
  23. ID int64
  24. Size string
  25. Color string
  26. Quantity int
  27. Images []string
  28. CreatedAt time.Time
  29. UpdatedAt time.Time
  30. CreatedBy uint
  31. UpdatedBy uint
  32. }

然后你应该在代码中使用Preload函数:

  1. err := p.DB.Preload("Groups.Items").Where("publication_date <= now()").Find(&products).Error
  2. if err != nil {
  3. return nil, err
  4. }
展开查看全部

相关问题