gorm: 为关系定义一个有效的外键,或者实现 Valuer/Scanner 接口。

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

gorm: define a valid foreign key for relations or implement the Valuer/Scanner interface

问题

我想在gorm中的模型中添加一些外键。我已经按照文档中添加外键的方法进行了操作。

以下是模型的代码:

Album模型:

type Album struct {
    gorm.Model
    Artists     []Artist
    Name        string
    ReleaseDate time.Time
    Genre       Genre
    Picture     string
}

Artist模型:

type Artist struct {
    gorm.Model
    Name        string
    Description string
    YearsActive string
}

Genre模型:

type Genre struct {
    gorm.Model
    Name        string
    Description string
}

Track模型:

type Track struct {
    gorm.Model
    Album    Album
    Name     string
    Artists  []Artist
    Playtime time.Duration
}

我用以下代码创建表:

DB.Debug().AutoMigrate(&models.Genre{}, &models.Artist{}, &models.Album{}, &models.Track{})

我在这里做错了什么?我对外键的概念还不太了解。我只是想将音轨与它们的艺术家和专辑关联起来。

英文:

I want to add some foreign keys to my models in gorm. I've done exactly the same as the documentation for adding foreign keys.
these are the models

Album model:

type Album struct {
    gorm.Model
    Artists     []Artist
    Name        string
    ReleaseDate time.Time
    Genre       Genre
    Picture     string
}

Artist model:

type Artist struct {
    gorm.Model
    Name        string
    Description string
    YearsActive string
}

Genre model:

type Genre struct {
    gorm.Model
    Name        string
    Description string
}

Track model:

type Track struct {
    gorm.Model
    Album    Album
    Name     string
    Artists  []Artist
    Playtime time.Duration
}

and the code I'm using for creating tables:

DB.Debug().AutoMigrate(&models.Genre{}, &models.Artist{}, &models.Album{}, &models.Track{})

what am I doing wrong here? I'm new to the foreign key concept. I just want to associate tracks with their artists and albums respectively.

答案1

得分: 3

所以,问题是gorm不知道如何将专辑与艺术家连接起来(其他情况类似),你需要在Artist结构体中添加AlbumID字段,以便gorm知道结构体之间的连接关系。

以下是你提供的所有结构体的示例:

Album模型:

type Album struct {
    gorm.Model
    Artists     []Artist `gorm:"many2many:album_artists;"`
    Name        string
    ReleaseDate time.Time
    GenreID     uint
    Genre       Genre
    Picture     string
}

由于专辑和艺术家是多对多的关系,你可以根据需要进行反向引用在这里查看

Artist模型:

type Artist struct {
    gorm.Model
    Name        string
    Description string
    YearsActive string
}

Genre模型:

type Genre struct {
    gorm.Model
    Name        string
    Description string
}

Track模型:

type Track struct {
    gorm.Model
    AlbumID  uint
    Album    Album
    Name     string
    Artists  []Artist  `gorm:"many2many:track_artists;"`  // 你也可以反向引用这个
    Playtime time.Duration
}

现在你可以使用以下代码:

DB.Debug().AutoMigrate(&models.Genre{}, &models.Artist{}, &models.Album{}, &models.Track{})
英文:

So, the issue is that gorm does not know how to connect album with artist (same for others), you need to add AlbumID field to Artist struct in order for gorm to know what is the connection between structs..

Here is the example for all structs that you presented:

Album model:

type Album struct {
    gorm.Model
    Artists     []Artist `gorm:"many2many:album_artists;"`
    Name        string
    ReleaseDate time.Time
    GenreID     uint
    Genre       Genre
    Picture     string
}

Since album and artist is many-to-many relationship, you may back-reference it depending on your needs check here

Artist model:

type Artist struct {
    gorm.Model
    Name        string
    Description string
    YearsActive string
}

Genre model:

type Genre struct {
    gorm.Model
    Name        string
    Description string
}

Track model:

type Track struct {
    gorm.Model
    AlbumID  uint
    Album    Album
    Name     string
    Artists  []Artist  `gorm:"many2many:track_artists;"`  // you may back reference this too
    Playtime time.Duration
}

And now you can use this:

DB.Debug().AutoMigrate(&models.Genre{}, &models.Artist{}, &models.Album{}, &models.Track{})

huangapple
  • 本文由 发表于 2022年2月17日 02:58:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/71147779.html
匿名

发表评论

匿名网友

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

确定