Gorm属于不返回关联关系。

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

Gorm belongs to does not return relations

问题

我有以下这些模型:

  1. type Message struct {
  2. gorm.Model
  3. UserID uint `json:"userId"`
  4. User userModels.User
  5. Content string `json:"content"`
  6. }
  7. type Receiver struct {
  8. gorm.Model
  9. UserID uint `json:"userId"`
  10. User userModels.User
  11. FirstName string `json:"firstName"`
  12. LastName string `json:"lastName"`
  13. Email string `json:"email"`
  14. Address string `json:"address"`
  15. Phone string `json:"phone"`
  16. }
  17. type Delivery struct {
  18. gorm.Model
  19. Message Message `json:"message"`
  20. MessageID uint `json:"messageId"`
  21. Receiver Receiver `json:"receiver"`
  22. ReceiverID uint `json:"receiverId"`
  23. DeliveryDate time.Time `json:"deliveryDate"`
  24. Sent bool `json:"sent"`
  25. }

我在数据库中插入了一些具有特定MessageID和ReceiverID的Delivery。当我查询该特定Delivery时,我期望得到相应的Message和具有ReceiverID的Receiver。

但是我得到的是一个既没有Message对象也没有Receiver对象的Delivery。

以下是我得到的结果:

  1. {
  2. "ID": 2,
  3. "CreatedAt": "2021-08-26T04:44:33.366628+04:30",
  4. "UpdatedAt": "2021-08-26T04:44:33.366628+04:30",
  5. "DeletedAt": null,
  6. "message": {
  7. "ID": 0,
  8. "CreatedAt": "0001-01-01T00:00:00Z",
  9. "UpdatedAt": "0001-01-01T00:00:00Z",
  10. "DeletedAt": null,
  11. "userId": 0,
  12. "User": {
  13. "ID": 0,
  14. "CreatedAt": "0001-01-01T00:00:00Z",
  15. "UpdatedAt": "0001-01-01T00:00:00Z",
  16. "DeletedAt": null,
  17. "firstName": "",
  18. "lastName": "",
  19. "email": "",
  20. "password": "",
  21. "country": "",
  22. "dateOfBirth": "0001-01-01T00:00:00Z",
  23. "rank": "",
  24. "gender": "",
  25. "plan": ""
  26. },
  27. "content": ""
  28. },
  29. "messageId": 2,
  30. "Receiver": {
  31. "ID": 0,
  32. "CreatedAt": "0001-01-01T00:00:00Z",
  33. "UpdatedAt": "0001-01-01T00:00:00Z",
  34. "DeletedAt": null,
  35. "userId": 0,
  36. "User": {
  37. "ID": 0,
  38. "CreatedAt": "0001-01-01T00:00:00Z",
  39. "UpdatedAt": "0001-01-01T00:00:00Z",
  40. "DeletedAt": null,
  41. "firstName": "",
  42. "lastName": "",
  43. "email": "",
  44. "password": "",
  45. "country": "",
  46. "dateOfBirth": "0001-01-01T00:00:00Z",
  47. "rank": "",
  48. "gender": "",
  49. "plan": ""
  50. },
  51. "firstName": "",
  52. "lastName": "",
  53. "email": "",
  54. "address": "",
  55. "phone": ""
  56. },
  57. "receiverId": 1,
  58. "deliveryDate": "2021-08-25T05:18:27.950457+04:30",
  59. "sent": false
  60. }

而我期望它返回我指定的messageId的Message和具有ReceiverID的Receiver。

英文:

I have these models

  1. type Message struct {
  2. gorm.Model
  3. UserID uint `json:"userId"`
  4. User userModels.User
  5. Content string `json:"content"`
  6. }
  7. type Receiver struct {
  8. gorm.Model
  9. UserID uint `json:"userId"`
  10. User userModels.User
  11. FirstName string `json:"firstName"`
  12. LastName string `json:"lastName"`
  13. Email string `json:"email"`
  14. Address string `json:"address"`
  15. Phone string `json:"phone"`
  16. }
  17. type Delivery struct {
  18. gorm.Model
  19. Message Message `json:"message"`
  20. MessageID uint `json:"messageId"`
  21. Receiver Receiver `json:"Receiver"`
  22. ReceiverID uint `json:"receiverId"`
  23. DeliveryDate time.Time `json:"deliveryDate"`
  24. Sent bool `json:"sent"`
  25. }

I insert some Delivery in the database with certain MessageID and ReceiverID. When I query that certain Delivery I expect to get the regarding message with the id of MessageID, and a Receiver of id ReceiverID.
But what I get is a Delivery with both empty Message and Receiver objects.

this is what I get:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. {
  2. &quot;ID&quot;: 2,
  3. &quot;CreatedAt&quot;: &quot;2021-08-26T04:44:33.366628+04:30&quot;,
  4. &quot;UpdatedAt&quot;: &quot;2021-08-26T04:44:33.366628+04:30&quot;,
  5. &quot;DeletedAt&quot;: null,
  6. &quot;message&quot;: {
  7. &quot;ID&quot;: 0,
  8. &quot;CreatedAt&quot;: &quot;0001-01-01T00:00:00Z&quot;,
  9. &quot;UpdatedAt&quot;: &quot;0001-01-01T00:00:00Z&quot;,
  10. &quot;DeletedAt&quot;: null,
  11. &quot;userId&quot;: 0,
  12. &quot;User&quot;: {
  13. &quot;ID&quot;: 0,
  14. &quot;CreatedAt&quot;: &quot;0001-01-01T00:00:00Z&quot;,
  15. &quot;UpdatedAt&quot;: &quot;0001-01-01T00:00:00Z&quot;,
  16. &quot;DeletedAt&quot;: null,
  17. &quot;firstName&quot;: &quot;&quot;,
  18. &quot;lastName&quot;: &quot;&quot;,
  19. &quot;email&quot;: &quot;&quot;,
  20. &quot;password&quot;: &quot;&quot;,
  21. &quot;country&quot;: &quot;&quot;,
  22. &quot;dateOfBirth&quot;: &quot;0001-01-01T00:00:00Z&quot;,
  23. &quot;rank&quot;: &quot;&quot;,
  24. &quot;gender&quot;: &quot;&quot;,
  25. &quot;plan&quot;: &quot;&quot;
  26. },
  27. &quot;content&quot;: &quot;&quot;
  28. },
  29. &quot;messageId&quot;: 2,
  30. &quot;Receiver&quot;: {
  31. &quot;ID&quot;: 0,
  32. &quot;CreatedAt&quot;: &quot;0001-01-01T00:00:00Z&quot;,
  33. &quot;UpdatedAt&quot;: &quot;0001-01-01T00:00:00Z&quot;,
  34. &quot;DeletedAt&quot;: null,
  35. &quot;userId&quot;: 0,
  36. &quot;User&quot;: {
  37. &quot;ID&quot;: 0,
  38. &quot;CreatedAt&quot;: &quot;0001-01-01T00:00:00Z&quot;,
  39. &quot;UpdatedAt&quot;: &quot;0001-01-01T00:00:00Z&quot;,
  40. &quot;DeletedAt&quot;: null,
  41. &quot;firstName&quot;: &quot;&quot;,
  42. &quot;lastName&quot;: &quot;&quot;,
  43. &quot;email&quot;: &quot;&quot;,
  44. &quot;password&quot;: &quot;&quot;,
  45. &quot;country&quot;: &quot;&quot;,
  46. &quot;dateOfBirth&quot;: &quot;0001-01-01T00:00:00Z&quot;,
  47. &quot;rank&quot;: &quot;&quot;,
  48. &quot;gender&quot;: &quot;&quot;,
  49. &quot;plan&quot;: &quot;&quot;
  50. },
  51. &quot;firstName&quot;: &quot;&quot;,
  52. &quot;lastName&quot;: &quot;&quot;,
  53. &quot;email&quot;: &quot;&quot;,
  54. &quot;address&quot;: &quot;&quot;,
  55. &quot;phone&quot;: &quot;&quot;
  56. },
  57. &quot;receiverId&quot;: 1,
  58. &quot;deliveryDate&quot;: &quot;2021-08-25T05:18:27.950457+04:30&quot;,
  59. &quot;sent&quot;: false
  60. }

<!-- end snippet -->

While I expect it to return the message with the messageId I specified and also the receiver

答案1

得分: 2

当我查询特定的交付时,你好。如果你使用Preload,你将获得接收者和消息的详细信息。如果你不使用Preload,你将得到一个包含空消息和接收者对象的交付对象。如果你只需要消息ID和接收者ID,你应该将Message Message更改为Message *Message,将Receiver Receiver更改为Receiver *Receiver。如果你使用Preload,结果与上述相同。如果你不使用Preload,接收者和消息将为nil。以下是所有的测试代码和调试信息:

  1. type User3 struct {
  2. gorm.Model
  3. Name string
  4. }
  5. type Message struct {
  6. gorm.Model
  7. UserID uint `json:"userId"`
  8. User *User3
  9. Content string `json:"content"`
  10. }
  11. type Receiver struct {
  12. gorm.Model
  13. UserID uint `json:"userId"`
  14. User *User3
  15. FirstName string `json:"firstName"`
  16. LastName string `json:"lastName"`
  17. Email string `json:"email"`
  18. Address string `json:"address"`
  19. Phone string `json:"phone"`
  20. }
  21. type Delivery struct {
  22. gorm.Model
  23. Message *Message `json:"message"`
  24. MessageID uint `json:"messageId"`
  25. Receiver *Receiver `json:"receiver"`
  26. ReceiverID uint `json:"receiverId"`
  27. DeliveryDate time.Time `json:"deliveryDate"`
  28. Sent bool `json:"sent"`
  29. }
  30. func main() {
  31. db := config.CreateMysql()
  32. db.AutoMigrate(Delivery{})
  33. d := Delivery{
  34. Message: &Message{
  35. User: &User3{Name: "a"},
  36. Content: "hello b~",
  37. },
  38. Receiver: &Receiver{
  39. User: &User3{Name: "b"},
  40. },
  41. DeliveryDate: time.Now(),
  42. Sent: false,
  43. }
  44. db.Create(&d)
  45. d2 := Delivery{}
  46. db.First(&d2)
  47. fmt.Printf("%v\n", d2)
  48. d3 := Delivery{}
  49. db.Preload("Message").Preload("Receiver").First(&d3)
  50. fmt.Printf("%v\n", d3)
  51. }

上述用户为nil,如果你想预加载嵌套关联,请使用以下方式:.Preload("Message.User")。详细信息请参考:https://gorm.io/docs/preload.html

英文:

When I query that certain Delivery

how do you do?

if you uses Preload
db.Preload(&quot;Message&quot;).Preload(&quot;Receiver&quot;).First(&amp;d3)

you will get the deltial of Receiver and message like

if you don't uses Preload
what you get is a Delivery with both empty Message and Receiver objects.

if you only need messageId and receiverID
you should change the Message Message to Message *Message and Receiver Receiver to Receiver *Receiver

if you uses Preload -> same to above

if you don't uses Preload -> Receiver and Message will be nil

follow is all test code and debug info:

  1. type User3 struct {
  2. gorm.Model
  3. Name string
  4. }
  5. type Message struct {
  6. gorm.Model
  7. UserID uint `json:&quot;userId&quot;`
  8. User *User3
  9. Content string `json:&quot;content&quot;`
  10. }
  11. type Receiver struct {
  12. gorm.Model
  13. UserID uint `json:&quot;userId&quot;`
  14. User *User3
  15. FirstName string `json:&quot;firstName&quot;`
  16. LastName string `json:&quot;lastName&quot;`
  17. Email string `json:&quot;email&quot;`
  18. Address string `json:&quot;address&quot;`
  19. Phone string `json:&quot;phone&quot;`
  20. }
  21. type Delivery struct {
  22. gorm.Model
  23. Message *Message `json:&quot;message&quot;`
  24. MessageID uint `json:&quot;messageId&quot;`
  25. Receiver *Receiver `json:&quot;Receiver&quot;`
  26. ReceiverID uint `json:&quot;receiverId&quot;`
  27. DeliveryDate time.Time `json:&quot;deliveryDate&quot;`
  28. Sent bool `json:&quot;sent&quot;`
  29. }
  30. func main() {
  31. db := config.CreateMysql()
  32. db.AutoMigrate(Delivery{})
  33. d := Delivery{Message: &amp;Message{User: &amp;User3{Name: &quot;a&quot;}, Content: &quot;hello b~&quot;}, Receiver: &amp;Receiver{User: &amp;User3{Name: &quot;b&quot;}}, DeliveryDate: time.Now(), Sent: false}
  34. db.Create(&amp;d)
  35. d2 := Delivery{}
  36. db.First(&amp;d2)
  37. fmt.Printf(&quot;%v\n&quot;, d2)
  38. d3 := Delivery{}
  39. db.Preload(&quot;Message&quot;).Preload(&quot;Receiver&quot;).First(&amp;d3)
  40. fmt.Printf(&quot;%v\n&quot;, d3)
  41. }

Gorm属于不返回关联关系。

ps: above user is nil, if you want preload nested associations, uses follow:
.Preload(&quot;Message.User&quot;)
https://gorm.io/docs/preload.html

huangapple
  • 本文由 发表于 2021年8月26日 08:33:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/68931208.html
匿名

发表评论

匿名网友

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

确定