不支持的关系类型,Has Many(UUID,无 Gorm 模型)

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

Unsupported relations for schema, Has Many (UUID, Without Gorm Model)

问题

当我尝试预加载,就像文档中描述的那样,我遇到了错误Cameras: unsupported relations for schema RecordingServer

我还在使用该模式进行JSON验证,并且不想使用Gorm模型,而是将Guid作为我的主键类型为uuid。我无法自己解决这个问题,所以任何帮助将不胜感激。当然,我已经阅读了其他帖子,但没有找到适合的解决方案。

  1. var rs []sdk.RecordingServer
  2. err := database.Session.Preload("Cameras").Find(&rs)
  3. fmt.Println(err, rs)
  4. // Cameras: unsupported relations for schema RecordingServer
  1. // Models
  2. type RecordingServerBase struct {
  3. Name string `json:"name" gorm:"size:128"`
  4. Address string `json:"address" gorm:"size:16"`
  5. Cameras []Camera `json:"cameras" gorm:"foreignKey:RecordingServerGuid;type:uuid"`
  6. }
  7. type RecordingServer struct {
  8. Guid uuid.UUID `json:"guid" format:"uuid" gorm:"type:uuid;primaryKey"`
  9. RecordingServerBase
  10. }
  11. type CameraBase struct {
  12. Name string `json:"name" gorm:"size:128"`
  13. Address string `json:"address" gorm:"size:16"`
  14. Username string `json:"username" gorm:"size:32"`
  15. Password string `json:"password" gorm:"size:64"`
  16. RecordingServerGuid uuid.UUID
  17. }
  18. type Camera struct {
  19. Guid uuid.UUID `json:"guid" format:"uuid" gorm:"type:uuid;primaryKey"`
  20. CameraBase
  21. }

当我只使用普通的SQL时,它可以正常工作:

  1. SELECT * FROM `cameras` WHERE recording_server_guid IN ('55c0b198-9ec3-4270-8030-77546ab40c19');

谢谢。

英文:

When I try to preload, like described in docs I get the error Cameras: unsupported relations for schema RecordingServer

I am unsing the schema also for json validation and dont want use Gorm Model and have Guid with type uuid as my primary keys. I am unable to come accross this issue by itself, so any help would be appreciated. Of course I read other posts but dont find a solution that fits.

  1. var rs []sdk.RecordingServer
  2. err := database.Session.Preload("Cameras").Find(&rs)
  3. fmt.Println(err, rs)
  4. // Cameras: unsupported relations for schema RecordingServer
  5. # Models
  6. type RecordingServerBase struct {
  7. Name string `json:"name" gorm:"size:128"`
  8. Address string `json:"address" gorm:"size:16"`
  9. Cameras []Camera `json:"cameras" gorm:"foreignKey:RecordingServerGuid;type:uuid"`
  10. }
  11. type RecordingServer struct {
  12. Guid uuid.UUID `json:"guid" format:"uuid" gorm:"type:uuid;primaryKey";`
  13. RecordingServerBase
  14. }
  15. type CameraBase struct {
  16. Name string `json:"name" gorm:"size:128"`
  17. Address string `json:"address" gorm:"size:16"`
  18. Username string `json:"username" gorm:"size:32"`
  19. Password string `json:"password" gorm:"size:64"`
  20. RecordingServerGuid uuid.UUID
  21. }
  22. type Camera struct {
  23. Guid uuid.UUID `json:"guid" format:"uuid" gorm:"type:uuid;primaryKey";`
  24. CameraBase
  25. }

When I do just a plain sql it works with

  1. SELECT * FROM `cameras` WHERE recording_server_guid IN ('55c0b198-9ec3-4270-8030-77546ab40c19');

Thanks

答案1

得分: 1

你应该将type:uuid放在字段定义中,而不是关系中。

如果我将其自动迁移到数据库中,它可以工作:

  1. type RecordingServerBase struct {
  2. Name string `json:"name" gorm:"size:128"`
  3. Address string `json:"address" gorm:"size:16"`
  4. Cameras []Camera `json:"cameras" gorm:"foreignKey:RecordingServerGuid"`
  5. }
  6. type RecordingServer struct {
  7. Guid uuid.UUID `json:"guid" format:"uuid" gorm:"type:uuid;primaryKey"`
  8. RecordingServerBase
  9. }
  10. type CameraBase struct {
  11. Name string `json:"name" gorm:"size:128"`
  12. Address string `json:"address" gorm:"size:16"`
  13. Username string `json:"username" gorm:"size:32"`
  14. Password string `json:"password" gorm:"size:64"`
  15. RecordingServerGuid uuid.UUID `gorm:"type:uuid"`
  16. }
  17. type Camera struct {
  18. Guid uuid.UUID `json:"guid" format:"uuid" gorm:"type:uuid;primaryKey"`
  19. CameraBase
  20. }

请注意,Find()不会返回错误。你可能想使用:

  1. err := db.Find(&rs).Error
英文:

You should put the type:uuid to the field definition, not the relation.

This works if I automigrate it to the db:

  1. type RecordingServerBase struct {
  2. Name string `json:"name" gorm:"size:128"`
  3. Address string `json:"address" gorm:"size:16"`
  4. Cameras []Camera `json:"cameras" gorm:"foreignKey:RecordingServerGuid"`
  5. }
  6. type RecordingServer struct {
  7. Guid uuid.UUID `json:"guid" format:"uuid" gorm:"type:uuid;primaryKey"`
  8. RecordingServerBase
  9. }
  10. type CameraBase struct {
  11. Name string `json:"name" gorm:"size:128"`
  12. Address string `json:"address" gorm:"size:16"`
  13. Username string `json:"username" gorm:"size:32"`
  14. Password string `json:"password" gorm:"size:64"`
  15. RecordingServerGuid uuid.UUID `gorm:"type:uuid"`
  16. }
  17. type Camera struct {
  18. Guid uuid.UUID `json:"guid" format:"uuid" gorm:"type:uuid;primaryKey"`
  19. CameraBase
  20. }

Be aware, that Find() does not return an error. You might want to use:

  1. err := db.Find(&rs).Error

huangapple
  • 本文由 发表于 2022年8月14日 14:02:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/73349434.html
匿名

发表评论

匿名网友

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

确定