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

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

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

问题

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

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

var rs []sdk.RecordingServer
err := database.Session.Preload("Cameras").Find(&rs)
fmt.Println(err, rs)
// Cameras: unsupported relations for schema RecordingServer
// Models
type RecordingServerBase struct {
    Name    string   `json:"name" gorm:"size:128"`
    Address string   `json:"address" gorm:"size:16"`
    Cameras []Camera `json:"cameras" gorm:"foreignKey:RecordingServerGuid;type:uuid"`
}

type RecordingServer struct {
    Guid uuid.UUID `json:"guid" format:"uuid" gorm:"type:uuid;primaryKey"`
    RecordingServerBase
}

type CameraBase struct {
    Name                string    `json:"name" gorm:"size:128"`
    Address             string    `json:"address" gorm:"size:16"`
    Username            string    `json:"username" gorm:"size:32"`
    Password            string    `json:"password" gorm:"size:64"`
    RecordingServerGuid uuid.UUID
}

type Camera struct {
    Guid uuid.UUID `json:"guid" format:"uuid" gorm:"type:uuid;primaryKey"`
    CameraBase
}

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

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.

var rs []sdk.RecordingServer
err := database.Session.Preload("Cameras").Find(&rs)
fmt.Println(err, rs)
// Cameras: unsupported relations for schema RecordingServer


# Models
type RecordingServerBase struct {
	Name     string    `json:"name" gorm:"size:128"`
	Address  string    `json:"address" gorm:"size:16"`
	Cameras  []Camera  `json:"cameras" gorm:"foreignKey:RecordingServerGuid;type:uuid"`
}

type RecordingServer struct {
	Guid uuid.UUID `json:"guid" format:"uuid" gorm:"type:uuid;primaryKey";`
	RecordingServerBase
}

type CameraBase struct {
	Name                 string   `json:"name" gorm:"size:128"`
	Address              string   `json:"address" gorm:"size:16"`
	Username             string   `json:"username" gorm:"size:32"`
	Password             string   `json:"password" gorm:"size:64"`
	RecordingServerGuid  uuid.UUID
}

type Camera struct {
	Guid uuid.UUID `json:"guid" format:"uuid" gorm:"type:uuid;primaryKey";`
	CameraBase
}

When I do just a plain sql it works with

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

Thanks

答案1

得分: 1

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

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

type RecordingServerBase struct {
    Name    string   `json:"name" gorm:"size:128"`
    Address string   `json:"address" gorm:"size:16"`
    Cameras []Camera `json:"cameras" gorm:"foreignKey:RecordingServerGuid"`
}

type RecordingServer struct {
    Guid uuid.UUID `json:"guid" format:"uuid" gorm:"type:uuid;primaryKey"`
    RecordingServerBase
}

type CameraBase struct {
    Name                string    `json:"name" gorm:"size:128"`
    Address             string    `json:"address" gorm:"size:16"`
    Username            string    `json:"username" gorm:"size:32"`
    Password            string    `json:"password" gorm:"size:64"`
    RecordingServerGuid uuid.UUID `gorm:"type:uuid"`
}

type Camera struct {
    Guid uuid.UUID `json:"guid" format:"uuid" gorm:"type:uuid;primaryKey"`
    CameraBase
}

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

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:

type RecordingServerBase struct {
	Name    string   `json:"name" gorm:"size:128"`
	Address string   `json:"address" gorm:"size:16"`
	Cameras []Camera `json:"cameras" gorm:"foreignKey:RecordingServerGuid"`
}

type RecordingServer struct {
	Guid uuid.UUID `json:"guid" format:"uuid" gorm:"type:uuid;primaryKey"`
	RecordingServerBase
}

type CameraBase struct {
	Name                string    `json:"name" gorm:"size:128"`
	Address             string    `json:"address" gorm:"size:16"`
	Username            string    `json:"username" gorm:"size:32"`
	Password            string    `json:"password" gorm:"size:64"`
	RecordingServerGuid uuid.UUID `gorm:"type:uuid"`
}

type Camera struct {
	Guid uuid.UUID `json:"guid" format:"uuid" gorm:"type:uuid;primaryKey"`
	CameraBase
}

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

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:

确定