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

Viewed 69

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 Answers

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
Related